Home > Mobile >  How to avoid HOME in the url an show only the view name?
How to avoid HOME in the url an show only the view name?

Time:02-09

Is there a way to avoid showing Home in the URL and only the name of the view?

I mean if I want to open the view About which is in the Home controller my URL will be like this:
www.website.com/Home/About

But I would love to show only
www.website.com/About

How can I do this?

CodePudding user response:

  1. You should then create a controller called 'About'.

  2. Create inside Controller About -> Action "Index".

  3. Copy the content from Home/About to About/Index.

If you want to go to more complicated way and it is not highly recommended (according to this discussion ) please see this solution

CodePudding user response:

You need to update your route configuration for the Home controller. If you're using Attribute-based routing with the following your Route configuration:

routes.MapMvcAttributeRoutes();

You can do the following in your Home controller:

// GET: Home
[Route("")]
public ActionResult Index()
{
  ViewBag.Title = "Home Page";

  return View();
}

[Route("about")]
public ActionResult About()
{
  ViewBag.Title = "About Page";
  ViewBag.Message = "About Us";

  return View();
}

That will then remove the controller name from the URLs as required.
  •  Tags:  
  • Related