Parameter value not passed in ASP.NET MVC route - asp.net-mvc

I'm learning about creating custom routes in ASP.NET MVC and have hit a brick wall. In my Global.asax.cs file, I've added the following:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
}
The idea is for me to able to navigate to http://localhost:123/home/filter/mynameparam. Here is my controller:
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Filter(string name)
{
return this.Content(String.Format("You found me {0}", name));
}
}
When I navigate to http://localhost:123/home/filter/mynameparam the contoller method Filter is called, but the parameter name is always null.
Could someone give a pointer as to the correct way for me to build my custom route, so that it passes the name part in the url into the name parameter for Filter().

The Default route should be the last one.
Try it this way:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// My Custom Route.
routes.MapRoute(
"User_Filter",
"home/filter/{name}",
new { controller = "Home", action = "Filter", name = String.Empty }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}

I believe your routes need to be the other way round?
The routes are processed in order, so if the first (default, OOTB) route matches the URL, that's the one that'll be used.

Related

How to get the add the query string to actionLink?

The below link gives be the following url: http://localhost:11111/files/Details/3
#Html.ActionLink("Details", "Details", "mycontroller", new { id = item.id },null)
But I'm trying to have a url parameter like this http://localhost:11111/files/Details?id=3 or http://localhost:11111/files/Details.aspx?id=3
How do I get the actionlink to show the url like details?i=3
Here is my controller View:
public ActionResult Details(int? id)
{
...
return View();
}
Why would you like to see the parameter's name in the link?
Asp.Net MVC uses user-friendly URLs.
If you have created a project in Visual Studio using the MVC template, probally your routes, by default, are configured to interpret the parameter after the controller/action/ like the id.
So the id parameter's value in your action will be automatically replaced, by model binding, with the id number present in you URL.
The routing codes can be found under the RegisterRoutes method in the Global.asax file of our project.
I see a cookie already in the RegisterRoutes method.
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults);
Using the MapRoute method above, we defined a new route.
Sample ;
public class HaberController : Controller
{
public ActionResult Listele()
{
// Listing codes will be written
return View("Listele");
}
public ActionResult Detay(string HaberId)
{
// Detail codes will be written
return View("Detay");
}
}
We go to our Global.asax file and edit it as follows.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"HaberListeleme",
"Haber",
new { controller = "Haber", action = "Listele" }
);
routes.MapRoute(
"HaberDetay",
"Haber/{id}",
new { controller = "Haber", action = "Detay" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
If we do the routing as follows:
routes.MapRoute(
"HaberDetay",
"Haber/{*Id}",
new { controller = "Haber", action = "Detay" }
);
So if we write * by putting the character next to our parameter name, it will be sent to the related parameter of the Detail method in the Controller, no matter what it says after the News / url tab.
For example:
http://www.doguhanaydeniz.com/Haber/Turkiye/Guncel/34389
If a URL is requested as Turkey / current / 34389 will be sent as a parameter.

MapRoute not working as expected

Considering the mapped routes below, when the user goes to /Home, why would the second route be hitting instead of the first? My assumption was that going to /Home would result in the first route being hit, but for some reason the second one is being used.
Why is this?
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Home page behind auth",
"Home",
new { controller = "Home", action = "HomeSecure", id = "" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
edit: just to be clear, I want /Home to go to the HomeSecure action in the Home controller.
edit2:
public ActionResult Index()
{
return View();
}
[AuthorizeHasAccess]
public ActionResult HomeSecure()
{
return View();
}
Given the information that you've given, I don't think that either route will match the request. MVC will be looking for method signatures with an "id" argument, and your action methods have none. If the request does in fact take you to the Index method on the Home controller, check to see if an error handler somewhere is trapping that condition and then redirecting you to /Home/Index
If you set the route table as follows, the first route will match as required.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Home page behind eRaider",
url: "Home",
defaults: new { controller = "Home", action = "HomeSecure" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", UrlParameter.Optional }
);
}

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.

ASP .Net custom route not working

ASP .Net custom route not working.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//default route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Account", action = "LogOn", id = UrlParameter.Optional } // Parameter defaults
);
//custom route
routes.MapRoute(
"Admin",
"Admin/{addressID}",// controller name with parameter value only(exclude parameter name)
new { controller = "Admin", action = "address" }
new { addressID = #"\d+" }
);
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
public ActionResult address(int addressID = 0)
{
//code and redirection
}
Here I want to hide everything from the url if possible...like i want to hide action name and parameter name and value if possible...
Suggest me the possible way to do this
Like I want URL like this (on priority basis)
1.http: //localhost:abcd/Admin
or
2.http: //localhost:abcd/Admin/address
or
3.http: //localhost:abcd/Admin/1
or
4.http: //localhost:abcd/Admin/address/1
for quick reference.
the custom route should appear before the default.
try naming your custom rout as null.
routes.MapRoute(
null, // Route name...
check that your calling the correct action.
if youre dealing with actions that dont recieve a parameter upon initial load(example paging)
makesure that your parameter is nullable address(int? addressID)
and on your custom route it should be like this
//custom route
routes.MapRoute(
null, //<<--- set to null
"Admin/{addressID}",// controller name with parameter value only(exclude arameter name)
new { controller = "Admin", action = "address" }
//new { addressID = #"\d+" } <<--- no need for this because based from your example " 2.http: //localhost:abcd/Admin/address" the parameter can be null.
);
thanks

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.

Resources