Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net core default route

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.

like image 454
Kovpaev Alexey Avatar asked Oct 20 '16 12:10

Kovpaev Alexey


People also ask

How do I set the default route in .NET Core Web API?

Right click your web project -> Select Properties -> Select the Debug tab on the left -> Then edit the 'Launch Url' field to set your own default launch url.

How would you setup the default route using endpoints?

UseEndpoints(endpoints => { endpoints. MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); }); Inside the call to UseEndpoints() , we use the MapControllerRoute() method to create a route by giving the name default .

What is conventional routing in ASP.NET Core?

Conventional routing: The route is determined based on conventions that are defined in route templates that, at runtime, will map requests to controllers and actions (methods). Attribute-based routing: The route is determined based on attributes that you set on your controllers and methods.

Where are routes defined in .NET Core?

Routing uses a pair of middleware, registered by UseRouting and UseEndpoints: UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline.


1 Answers

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.

like image 65
mybirthname Avatar answered Sep 19 '22 09:09

mybirthname