Change url asp.net mvc 5 - asp.net-mvc

routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
my routemap and i want my url in product action be like = http://localhost:13804/Wares/Product/name-id
but now is like =
http://localhost:13804/Wares/Product/4?name=name

When defining a route pattern the token { and } are used to indicate a parameter of the action method. Since you do not have a parameter called Product in your action method, there is no point in having {Product} in the route template.
Since your want url like yourSiteName/Ware/Product/name-id where name and id are dynamic parameter values, you should add the static part (/Ware/Product/) to the route template.
This should work.
routes.MapRoute(
name: "MyRoute",
url: "Ware/Product/{name}-{id}",
defaults: new { controller = "Ware", action = "Product",
name = UrlParameter.Optional, id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
Assuming your Product action method accepts these two params
public class WareController : Controller
{
public ActionResult Product(string name, int id)
{
return Content("received name : " + name +",id:"+ id);
}
}
You can generate the urls with the above pattern using the Html.ActionLink helper now
#Html.ActionLink("test", "Product", "Ware", new { id = 55, name = "some" }, null)

I know its late but you can use built-in Attribute Routing in MVC5. Hope it helps someone else. You don't need to use
routes.MapRoute(
name: "MyRoute",
url: "{Product}/{name}-{id}",
defaults: new { controller = "Home", action = "Product", name = UrlParameter.Optional , id = UrlParameter.Optional }
);
Instead you can use the method below.
First enable attribute routing in RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
}
Then in WaresController
[Route("Wares/Product/{name}/{id}")]
public ActionResult Product(string name,int id)
{
return View();
}
Then to navigate write code like this in View.cshtml file
Navigate
After following above steps your URL will look like
http://localhost:13804/Wares/Product/productname/5

Related

Asp.net Routes with two routes

I have two route with same signature but only the parameter names are different how to fix this issue.
Following is my code
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "DefaultRoute2",
url: "{controller}/{action}/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
The routes are meant to accept different URL structures. Both of your routes have same structure, so the first one will always match, and the second will never be tested.
Instead of using a different route, in /Employee/Index you should just use the parameter id.
public class EmployeeController : Controller
{
public ActionResult Index(string id)
{
string formSubmissionId = id;
}
}
The URL for that action would be the same that (I believe) you wanted to achieve with the second route: Employee/Index/id
UPDATE
I've just realized. If you only need the parameter formSubmissionId for the action /Employee/Index you could do this:
// Note the order of the routes:
routes.MapRoute(
name: "DefaultRoute2",
url: "Employee/Index/{formSubmissionId}",
defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);
public class EmployeeController : Controller
{
public ActionResult Index(string formSubmissionId)
{
// ...
}
}
Now i fix this issue as
routes.MapRoute(
name: "DefaultActivity",
url: "{controller}/LoadActivity/{formSubmissionId}",
defaults: new { controller = "{controller}", action = "LoadActivity", formSubmissionId = UrlParameter.Optional }
);
this is work around of my scenario.

Multiple optional parameters in MVC is not working

My requirement is to provide optional parameters to urls. urls should be like the.
http://test.com/118939
http://test.com/118939/test/2000/
I have written following routes
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?favicon.ico(/.*)?" });
routes.MapRoute(
"FAQDefault",
"FAQ",
new { controller = "FAQ", action = "Default" });
routes.MapRoute(null, "{id}", new { controller = "Home", action = "Default", id = UrlParameter.Optional });
routes.MapRoute("rent", "{id}/{rent}/{unit}", new { controller = "Home", action = "Default", id = UrlParameter.Optional, rent = UrlParameter.Optional, unit = UrlParameter.Optional });
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Default", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "CDCPortal" });
}
and controller written like:
public ActionResult Default(string id, string rent=null,string unit=null){}
Its working fine for 1 url but not working for second url.
There is no need to do as, you have done in the first case:
The second route can handle any number of parameters.
You need to define a route for each combinations like below
routes.MapRoute("Default-AllOptional",
"Default/{id}/{rent}/{unit}",
new
{
controller = "Home",
action = "Default"
// nothing optional
}
);
routes.MapRoute("Defaul-Id-rent-Optional",
"Default/{id}/{rent}",
new
{
controller = "Home",
action = "Default",
id=UrlParameter.Optional,
rent=UrlParameter.Optional
}
);
Refer Routing Regression With Two Consecutive Optional Url Parameters

can not map a route for a specific controller in mvc 4

I have a controller named Registration and an action method in it as the following:
public JsonResult GetReqs(GridSettings gridSettings, int rt)
{
...//whatever
return Json(jsonData, JsonRequestBehavior.AllowGet);
}
So I added a route and now my RouteConfig.cs is like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "RegReq",
url: "{Registration}/{GetReqs}/{rt}",
defaults: new { controller = "Registration", action = "GetReqs", rt = UrlParameter.Optional }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
However I can't access the rt parameter and the action method GetReqs isn't called(I set a breakpoint on it but nothing happened). Where is the mistake?
Edit: Link example I tried : ~/Registration/GetReqs/1
I think you need to remove the brackets in your first route:
routes.MapRoute(
name: "RegReq",
url: "Registration/GetReqs/{rt}",
defaults: new { controller = "Registration", action = "GetReqs",
rt = UrlParameter.Optional }
);
The default route has {controller} to tell MVC to use the string in that section of the url as the controller name. You know the controller, so you just need to match the specific string.

MVC3 Url Routing issue

I am trying to register a route as follows :
routes.MapRoute(
"SaleReport", // Route name
"SaleReport/GetDataConsolidated/{type}",
new { controller = "SaleReport",
action = "GetDataConsolidated",
type = UrlParameter.Optional});
and in controller
public ActionResult GetDataConsolidated(string type)
{
return Content("Report Type = " + type);
}
i am calling it like : localhost:56674/SaleReport/GetDataConsolidated/Sale
but the problem is the value of type is always null.
what am i doing wrong ?
It's probably just order of .MapRoute(...) calls.
Put your "SaleReport" .MapRoute(...) call before "Default" {controller}/{action} .MapRoute(...) call, since it's more specific.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SaleReport",
url: "SaleReport/GetDataConsolidated/{type}",
defaults: new { controller = "SaleReport", action = "GetDataConsolidated", type = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Is there any specific need to define another map route?
It should work with default route,
routes.MapRoute(
"SaleReport", // Route name
"SaleReport/GetDataConsolidated/{type}",
new { controller = "SaleReport",
action = "GetDataConsolidated",
type = UrlParameter.Optional});
Remove Above route,
Just change action methos like below
public ActionResult GetDataConsolidated(string id)
{
return Content("Report Type = " + id);
}
This will work,Thanks.

User routing in ASP.NET MVC for urls like www.website.com/users/jeffAtwood

I am trying to show user details at the following url :
www.website.com/users/yasser
where the last entry yasser is the username I have tried a couple of routes but it just does nt work.
My User controller is as shown below.
public class UserController : Controller
{
public ActionResult Index(string username)
{
var model = _service.GetUserDetails(username);
return View(model);
}
}
I have reffered this and couple of other links, but I really could not figure out how it worked.
Can some one help me out on this. Thanks
Edit :
My current route config is below
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Routes executes from top to the bottom:
routes.MapRoute("UserProfile",
"Users/{username}",
new { controller = "User", action = "Index", username = string.Empty }
);
routes.MapRoute("Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);

Resources