20

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"

ekad
  • 14,436
  • 26
  • 44
  • 46
Cagatay
  • 333
  • 1
  • 3
  • 13

6 Answers6

29

You need to use Attribute Routing, a feature introduced in MVC 5.

Based on your example you should edit your controller as follows:

[RoutePrefix("example-name")]
public class example_nameController : Controller
{
    // Route: example-name/Index
    [Route]
    public ActionResult Index()
    {
        return View();
    }

    // Route: example-name/Contact
    [Route]
    public ActionResult Contact()
    {
        ViewBag.Message = "Your contact page.";

        return View();
    }
}

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:

using AttributeRouting;
using AttributeRouting.Web.Mvc;


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:
@Html.ActionLink("Go to example-name2", "Index", "example_name2");

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:

public ActionResult RedirectToExample_Name2Controller()
{
    return RedirectToAction("Index", "example_name2");
}
user449689
  • 3,142
  • 4
  • 19
  • 37
  • thanks for detailed answer. it's better and it helped me. i have another question. I have different controller, let's say "example_controller2", when i click "Contact" then i want to go "example_controller2" I searched and i think i will use "RedirectToRoute". It's kind of "RedirectToAction" but i didn't make it work. – Cagatay Oct 27 '15 at 12:32
  • I know this is old, but I needed to include `using RoutePrefixAttribute = AttributeRouting.RoutePrefixAttribute;` to clarify ambiguity with the System Route Prefix Attribute (for MVC < 5) – koolahman Jul 10 '19 at 13:11
16

You can do this via the Routes.cs

routes.MapRoute(
      name: "Controller",
      url: "example-controller/{action}",
      defaults: new { 
      controller = "ControllerName", action ="Index"
      }   
);

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?

Community
  • 1
  • 1
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
13

user449689s answer is good, but he forgot to mention you need to add

routes.MapMvcAttributeRoutes();

into RegisterRoutes() of your RouteConfig.cs

Murphybro2
  • 2,207
  • 1
  • 22
  • 36
  • 1
    Nice addition, it's also worth mention that this line must be placed before any `routes.MapRoute` call in order for it to work. – AarónBC. Jun 28 '18 at 03:09
4

You can use Attribute Routing.

[RoutePrefix("Users")]
public class HomeController : Controller
{
    //Route: Users/Index
    [Route("Index")]
    public ActionResult Index()
    {
        return View();
    }
}
Givi
  • 1,674
  • 2
  • 20
  • 35
2

you can specified in Routes.cs

 routes.MapRoute(
 name: "College",
 url: "Student/{studentId}",
 defaults: new { controller = "Student", action = "Details"}
 );

We can define such a constraint as

  routes.MapRoute(
  name: "College",
  url: "Student/{studentId}", 
  defaults: new { controller = "Student", action = "Details"},
  constraints:new{id=@"\d+"} 
  ); 
Lalji Dhameliya
  • 1,729
  • 1
  • 17
  • 26
  • routes.MapRoute( "ClientDetailLevel1", "Clients/{id}", new { controller = "Clients", action = "Index" }); – Arfat Dec 16 '19 at 07:59
0

Open up your RouteConfig.cs class and add in the following to the RegisterRoutes method:

  routes.MapMvcAttributeRoutes();

Then, above the Controller method you would like a custom route for, add this attribute or similar:

  [Route("MyCustomRoute/MoreCustomRouting")]
JsonStatham
  • 9,770
  • 27
  • 100
  • 181