vaindil vaindil - 1 year ago 86
C# Question

Routing doesn't work without attributes

EDIT: An issue has been opened on GitHub to clarify the docs with the information in the answer.

I'm trying to set up routing in my .NET Core MVC app (for an API). I believe I have everything configured correctly, but no routes work (all return 404) unless the action has a routing attribute explicitly set. This same problem is mentioned here, but he doesn't know what fixed the problem.

I put the attributes back on, it worked. I removed them, it didn't. Eventually through some magical incantation of removing and re-adding route configuration - switching it off and back on again in other words -
worked without routing attributes. Not sure what happened there.

Here is a simplified version of what I have. What's the problem? Why isn't routing working without attributes set?

In this example, I'm attempting to


public void ConfigureServices(IServiceCollection services)
services.AddMvc(config =>
var policy = new AuthorizationPolicyBuilder()
config.Filters.Add(new AuthorizeFilter(policy));
.AddJsonOptions(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());

public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
if (env.IsDevelopment())



I also tried specifying the route manually:

app.UseMvc(routes =>

Login controller:

public class LoginController : Controller
[Route("register")] // only works with this here
public IActionResult Register([FromBody]RegisterModel model)
return Ok();

Answer Source

The default {controller=Home}/{action=Index}/{id?} route will already map the controller and action to /Login/Controller. However, if you add a [Route] attribute on the controller, you indicate that you want to start building a new route and the default route will not apply anymore. Therefore you'll either have to remove the attributes from both the controller and action or add it to both. You might also want to use the [action] route token as well:

public class LoginController : Controller
   // ...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download