Reputation: 23
I have Login Controller and Register ActionResult
public ActionResult Register()
{
return View();
}
If i post any data to Register actionresult url seems like below,
WebSiteName/Login/Register
I want to change route url as WebSiteName/Login/Reg=Id?
So i tried below however i could not change route url.
routes.MapRoute(
name: "something",
url: "Login/Reg=Id?",
defaults: new
{
controller = "Login",
action = "Register",
id = id = UrlParameter.Optional
}
);
So how can i change url in asp.net mvc ?
Any help will be appreciated.
Thanks.
Upvotes: 1
Views: 21535
Reputation: 1571
Here I've remove last two passing parameter from the URL
this is my url link -> http://localhost:12345/User?value=98998?id=2
and I want to remove value and id parameter from the url link
Modify your Routeconfig.cs like this
routes.MapRoute(
"User",
"User/{value}/{id}",
new { controller = "User", action = "Index", value= UrlParameter.Optional, id = UrlParameter.Optional }
);
User Controller
public ActionResult Index(string value, int id)
{
// write Your logic here
return view();
}
Create an Index view for UserController
@Html.ActionLink("LinkName", "Index", "User", new {value = "98998", id = "2"},null)
Finally we got a result: http://localhost:12345/User/98998/2
Likewise we can remove multiple parameter from the url
Upvotes: 0
Reputation: 56688
You are trying to use incorrect form of a url parameter. The options you have are:
Url part parameter: WebSiteName/Login/Reg/{id}
For this you can use following config
routes.MapRoute(
name: "something",
url: "Login/Reg/{id}",
defaults: new
{
controller = "Login",
action = "Register",
id = UrlParameter.Optional
}
);
Query string parameter: WebSiteName/Login/Reg?id={id}
Here you do not need to specify the parameter in config at all:
routes.MapRoute(
name: "something",
url: "Login/Reg",
defaults: new
{
controller = "Login",
action = "Register"
}
);
Of course in both cases it is assumed your action Register
has parameter id.
Upvotes: 6