If we have "example_name" we can change it in url using [ActionName("")] So, i want to do this for controller name.
I can do this:
ControllerName > example_nameController > in URL: "/example_controller"
I would like to change controller name like this in URL: "/example-conroller"
You can use Attribute Routing.
You can do this via the
Routes.cs
There is also another way, if you look at the answer of this question: How to achieve a dynamic controller and action method in ASP.NET MVC?
you can specified in Routes.cs
We can define such a constraint as
user449689s answer is good, but he forgot to mention you need to add
into RegisterRoutes() of your RouteConfig.cs
You need to use Attribute Routing, a feature introduced in MVC 5.
Based on your example you should edit your controller as follows:
Using the
RoutePrefix
attribute on top of your controller will allow you to define the route on the entire controller.As said before, this feature is available natively in MVC 5, if you are using a previous version of MVC you need to add the following NuGet package: AttributeRouting and add the following using in your controller:
If you have another controller called
example_name2Controller
and you want to add an hyperlink that link to it you can easily do it as follows:You don't need to call an action that will redirect to the
example_name2Controller
, but if you need to do it in other occasions, you can do it like this: