Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change ASP.NET MVC Controller Name in URL?

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"

like image 610
Cagatay Avatar asked Oct 26 '15 11:10

Cagatay


People also ask

How do you give a controller a URL?

UrlHelper u = new UrlHelper(this. ControllerContext. RequestContext); string url = u. Action("About", "Home", null);

How do I change the default URL in MVC?

How do I change the default page in ASP NET MVC? You can set up a default route: routes. MapRoute( "Default", // Route name "", // URL with parameters new { controller = "Home", action = "Index"} // Parameter defaults ); Just change the Controller/Action names to your desired default.

What is URL rewriting in MVC?

URL rewriting is the concept of changing the currently executing URL and pointing it at some other URL to continue processing the current request or redirecting to an external URL.


2 Answers

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");
}
like image 86
user449689 Avatar answered Sep 19 '22 02:09

user449689


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?

like image 21
Jamie Rees Avatar answered Sep 22 '22 02:09

Jamie Rees