简体   繁体   中英

How do override ASP.NET Core's controller naming convention?

I'm using ASP.NET Core. The convention is that the routing system considers a FooController class to be a controller with the name Foo .

I need to override this convention. I want to do something like this:

[ControllerName("Bar")]
public class SomeArbitraryName : Controller {
}

Or like this:

public class SomeArbitraryName : Controller("Bar") {
}

Is that possible somehow?

EDIT: No that linked "duplicate" question is not for ASP.NET Core!

Attribute routing still exists in Asp.Net Core

[Route("Bar")]
public class SomeArbitraryName : Controller
{
    // ...
}

See Documentation: Routing to Controller Actions - Attribute Routing

You could implement your own System.Web.Mvc.IControllerFactory and do ControllerBuilder.Current.SetControllerFactory(new MyImpl()) at some point in the application initialization step

Edit: This advice applies to ASP.NET MVC 5 and Core might have different interfaces for something similar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM