how can i simplify url in .NET MVC?


How can I simplify the url to http://localhost:51675/Prescriptions/Create/12 instead of http://localhost:51675/Prescriptions/Create?AssessmentID=12.
currently i am using this code

@Html.ActionLink("Edit", "Edit", new { id = Model.PrescriptionID })

as asked on Facebook group


Asked by:- Vinnu
1
: 3447 At:- 6/27/2017 10:24:02 AM
C# MVC ASP.NET routing







1 Answers
profileImage Answered by:- vikas_jk

You can do this by following methods

Method 1:

1. Go to App_Start folder then RouteConfig.Cs  file 

2. Add new Route in it , like this

routes.MapRoute(
name:"UniqueRouteName",
Url:"{controller}/{action}/{id}",//this is url pattern which we want, in your case it would be "/Edit/Edit/12"
defaults:new{controller="Edit",action="Edit",id=UrlParameter.Optional}
);

3. Build your solution and you are done.

Note: This Should be placed before Default routing

Method 2:

  1. Open controller[Edit] file.
  2. Go to ActionMethod for which you want routing
  3. Add a route attribute, something like this

    [Route(“Edit/{id:int}”)]
    public ActionResult Edit(int id)
    {
     //do something
     return View();
    }
    ?
  4. To Enable attribute routing you need to go RouteConfig.cs and add this code in RegisterRoute Method

    routes.MapMvcAttributeRoutes();?
  5. Build and run your code.

That's it, hope this helps

2
At:- 6/27/2017 10:44:02 AM Updated at:- 6/27/2017 10:45:00 AM
more details on https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/ 0
By : vikas_jk - at :- 6/27/2017 10:45:51 AM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use