28

So I have a HomeController, to access it along with Actions I have to type url.com/home/action.

Would it be possible to change this to something else like url.com/anothernamethatpointstohomeactually/action?

5 Answers 5

58

I suggest you to use attribute routing, but of course it depends on your scenario.

[Route("prefix")]
public class Home : Controller {

    [HttpGet("name")]
    public IActionResult Index() {
    }

}

This will be found at url.com/prefix/name

There are a lot of options to attribute routing, some samples:

[Route("[controller]")] // there are placeholders for common patterns 
                           as [area], [controller], [action], etc.

[HttpGet("")] // empty is valid. url.com/prefix

[Route("")] // empty is valid. url.com/

[HttpGet("/otherprefix/name")] // starting with / won't use the route prefix

[HttpGet("name/{id}")]
public IActionResult Index(int id){ ... // id will bind from route param.

[HttpGet("{id:int:required}")] // you can add some simple matching rules too.

Check Attribute Routing official docs

Sign up to request clarification or add additional context in comments.

Comments

3

You can add new Routes in your Startup.Configure method within your app.UseMvc(routes => block:

routes.MapRoute(
    name: "SomeDescriptiveName",                      
    template: "AnotherNameThatPointsToHome/{action=Index}/{id?}",
    defaults: new { controller = "Home"} 
);

The code is quite similar to ASP.NET MVC.

For more info, see Routing in ASP.NET Core.

Below is for ASP.NET MVC (not ASP.NET Core MVC)

You can also add a new Route via routes.MapRoute in your RouteConfig:

routes.MapRoute(
    name: "SomeDescriptiveName",
    url: "AnotherNameThatPointsToHome/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Make sure, you insert the code before you define your Default route.

For more information, visit the docs.

1 Comment

In .net core 3.1. you need to use app.useEndPoints();
3

In ASP.NET Core 6, we just do that in one line.

Go to your Controller and write before your action method:

[Route("YourController/YourAction/{YourParameter?}")]

In your example, you you need to write like this:

[Route("Home/Index/{name?}")]

Comments

1

Using the Route attribute on top of your controller will allow you to define the route on the entire controller. [Route("anothernamethatpointstohomeactually")]

You can read more here.

Comments

-1

You can change your url by modifying your routing configuration. It is kind of like htaccess but not really. https://learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions-1/controllers-and-routing/creating-custom-routes-cs

Another solution is to create a page and do a server redirect.

Server.Transfer

2 Comments

Server.Transfer does not exist in asp.net core mvc.
Your link seems to be broken.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.