MiP
MiP

Reputation: 6422

Routing with friendly URLs

For example, I have an About Us page:

htttp://localhost/About/Index

How can I have an user-friendly URL for this page?

http://localhost/about-us.html

My current workaround:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvc(routes =>
    {
        routes.MapRoute(
            name: "AboutPage",
            template: "about-us.html",
            defaults: new {controller = "About", action = "Index"});
    });
}

This is the wrong way to do. How can I map routing correctly so that when I use @Url.Action("Index", "About") in cshtml page, it'll generate the link /about-us.html for me?

Upvotes: 2

Views: 373

Answers (1)

Afshar
Afshar

Reputation: 11483

You can use custom URL like this:

[Route("about-us.html")]
public IHttpActionResult Index()
{
    return Ok("About us");
}

Upvotes: 1

Related Questions