Asp.net core default route

不想你离开。 提交于 2020-12-30 05:12:50

问题


Simplified Startup code:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
        name: "default",
        template: "",
        defaults: new { controller = "Main", action = "Index" });
    });
}

After running application in Visual Studio 2015 I see in browser "localhost:xxx", but I don't see result of MainController.Index(). Just blank page. What did I miss?

Update:

Web.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false"/>
  </system.webServer>
</configuration>

Update 2:

The problem comes from exception in dependency injected service to controller and because I forget to use developer exception page site just returned blank page to me. So I'm sorry for incorrect question, but routing is fine in my case.


回答1:


routes.MapRoute(
    name: "default",
    template: "{controller}/{action}/{id?}",
    defaults: new { controller = "Main", action = "Index" });

routes.MapRoute(
    name: "default",
    template: "{controller=Main}/{action=Index}/{id?}");

These are the two ways of defining default route. You are mixing them. You need always to define a template. In the second way you can write the defaults directly in the template.




回答2:


The easiest way for me (and without using MVC) was to set the controller to default route using empty [Route("")] custum attribute like so:

[ApiController]
[Route("")]
[Route("[controller]")]
public class MainController : ControllerBase
{ ... }

with Startup.Configure

app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});



回答3:


For all of you who get blank page set PreserveCompilationContext to true:

  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PreserveCompilationContext>true</PreserveCompilationContext>
  </PropertyGroup>

in csproj in vs 2017 or

"buildOptions": {   "preserveCompilationContext": true }

in project.json




回答4:


In Startup.cs class, use a convenient Method: UseMvcWithDefaultRoute():

public void Configure(IApplicationBuilder app, IHostingEnvironment 
{
   app.UseMvcWithDefaultRoute();
}

Can be used to change:


public void Configure(IApplicationBuilder app, IHostingEnvironment 
{
   app.UseMvc(routes =>
   {
      routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
   });
}

More info in Microsoft documentation



来源:https://stackoverflow.com/questions/40155182/asp-net-core-default-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!