Home > database >  How to hide unused id from URL routing in MVC?
How to hide unused id from URL routing in MVC?

Time:10-08

In action my parameter can be empty. So how can I hide if it is null . I want to handled on controller side.

[Route("2")]
[HttpGet]        
public ActionResult Index(string eID)
{
    return View();
}

The URL I'm getting like /2?eID. My eID can be null. I want to hide if eID is null in the URL. I'm accessing this action using var url = '@Url.Action("action", "Controller",new { eID = "sampleURI"})'; from another action view.

CodePudding user response:

Define the following route:

[HttpGet("/2/{eID?}")]
public ActionResult Index(string eID)
{
   return View();
}

The ? character in {eID?} defines eID as optional.

Therefore, you can use the following URLs:

  • /2
  • /2/some-text
  • /2?eID=some-text

An additional information you can find here: Routing in ASP.NET Core

  • Related