ASP.NET Core React模板,具有在同一项目中的MVC控制器和视图。

huangapple go评论62阅读模式
英文:

ASP.NET Core React Template with MVC Controllers & Views in same project

问题

Started with the ASP.Net Core with React (v6.0) template and am trying to add MVC Controllers & Views.

Have Added services.AddControllersWithViews() in ConfigureServices and the following is my Configure code:

 if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Error");
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseEndpoints(endpoints =>
{
    // Catchall Route for our API Controllers
    endpoints.MapControllerRoute(name: "default", pattern: "{controller}/{action}/{id?}");
});

app.Run();

I can call API Controllers (i.e. weatherforecast and sample api) but when I add HomeController and then add /Views/Home/Index.cshtml I can never get it to route to the razor page - only api calls work.

I cannot find an example that shows both MVC Views and React Spa running in the same solution.

TIA!

英文:

Started with the ASP.Net Core with React (v6.0) template and am trying to add MVC Controllers & Views.

Have Added services.AddControllersWithViews() in ConfigureServices and the following is my Configure code:

 if (!app.Environment.IsDevelopment())
            {
                app.UseExceptionHandler("/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();

            app.UseStaticFiles();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                // Catchall Route for our API Controllers
                endpoints.MapControllerRoute(name: "default", pattern: "{controller}/{action}/{id?}");
                                
            });            

            app.Run();

I can call API Controllers (i.e. weatherforecast and sample api) but when I add HomeController and then add /Views/Home/Index.cshtml I can never get it to route to the razor page - only api calls work.

I cannot find an example that shows both MVC Views and React Spa running in same solution.

TIA!

答案1

得分: 1

只返回翻译好的部分:

"OK folks... the key here is to simply include the MVC routes in the setupProxy.js"

翻译成中文为:

"好的,各位...关键是在setupProxy.js中简单地包含MVC路由。"

英文:

OK folks... the key here is to simply include the MVC routes in teh setupProxy.js

huangapple
  • 本文由 发表于 2023年3月4日 05:21:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75631985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定