asp.net mvc routing id parameter - asp.net

I am working on a website in asp.net mvc. I have a route
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
which is the default route. And I have a method
public ActionResult ErrorPage(int errorno)
{
return View();
}
Now if I want to run this code with http://something/mycontroller/Errorpage/1
it doesn't work. But if I change the parameter name to id from errorno
it works.
Is it compulsory to have same parameter name for this method? Or do I need to create separate routes for such situations?

So, you have a parameter named errorno, and you want it to have a value from parameter id. This is obviously the binding problem.
How to solve it:
create a class for model binder:
public class ParameterBinder : IModelBinder
{
public string ActualParameter { get; private set; }
public ParameterBinder(string actualParameter)
{
this.ActualParameter = actualParameter;
}
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
object id = controllerContext.RouteData.Values[this.ActualParameter];
return id;
}
}
create a custom attribute for custom model binding:
[AttributeUsage(AttributeTargets.Parameter)]
public class BindParameterAttribute : CustomModelBinderAttribute
{
public string ActualParameter { get; private set; }
public BindParameterAttribute(string actualParameter)
{
this.ActualParameter = actualParameter;
}
public override IModelBinder GetBinder()
{
return new ParameterBinder(this.ActualParameter);
}
}
apply the new attribute to your action parameters as needed:
public ActionResult ErrorPage(
[BindParameter("id")]
int errorno)
{
return View();
}
Now your errorno will have the value, which was passed as id for your url.
Note: you can remove the paramter id from the example above, if you are sure you need it solved only for id.
Leaving this way will allow you bind other parameters too.

Option 1
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public ActionResult ErrorPage(int id)
{
return View();
}
Option 2
routes.MapRoute(
"Default",
"{controller}/{action}/{errorno}",
new { controller = "Home", action = "Index", errorno = UrlParameter.Optional }
);
public ActionResult ErrorPage(int errorno)
{
return View();
}
Option 3
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
public ActionResult ErrorPage(int id)
{
int errorno = id;
return View();
}

use the bind attribute prefix:
public ActionResult Customer([Bind(Prefix = "id")]string cname) {}

#Parminder
The default route can handle all action with one parameter "id". And I think not every action need this parameter. So I change my default route
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index"}
);
and you can add a new route:
routes.MapRoute("errorpage", "yourcontroller/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
this just handle your controll name is "controllername". If you want to handle all controller, you can add this:
routes.MapRoute("errorpage", "{controller}/errorpage/{errorno}",
new {controller="controllername", action="errorpage"});
This method will create very much code in global.asax if you need a lot of custom route.

You could either rename the parameter in the default root (which probably is not a good idea) or rename it in the action method. Adding another root will not help because it will be the same as the default one and given an url the routing engine cannot distinguish between the two and will always take the first one in the list.

try to use the same name of parameter in action method as in in the route table url parameter.
global.asx
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
myController
public ActionResult ErrorPage(int id)
{
return View();
}

Related

Route Confusion with Single Action

I am clearly having issue understanding the routes here or trying to achieve which is not possible.
I have this in my RouteConfig:
routes.MapRoute(
name: "Explorer",
url: "{controller}/{action}/{prefix}/{value}",
defaults: new { controller = "Explorer", action = "Index", prefix ="", value = UrlParameter.Optional }
);
And in Explorer Controller I have the following:
[RoutePrefix("Explorer")]
public class ExplorerController : Controller
{
public ActionResult Index()
{
return View();
}
[Route("id/{value}")]
public ActionResult Import(decimal? importId)
{
return View();
}
[Route("text/{value}")]
public ActionResult Import(string importWindow)
{
return View();
}
}
One of the ACTION (Import) has different PREFIX such as window and id but whenever I try to access it (such as https://localhost/Explorer/Import/window/helloWorld OR https://localhost/Explorer/Import/id/200) it keeps giving me the following error
The current request for action 'Import' on controller type
'ExplorerController' is ambiguous between the following action
methods: System.Web.Mvc.ActionResult
Import(System.Nullable`1[System.Decimal]) on type
projectname.Controllers.ExplorerController System.Web.Mvc.ActionResult
Import(System.String) on type projectname.Controllers.ExplorerController
I know it is ambiguous but I have a prefix which should make it non-ambiguous.
What am I doing wrong here and how to achieve this outcome if this is not the right way to do it?
Apply the [Route()] attribute for action methods in the controller class:
[RoutePrefix("Explorer")]
public class ExplorerController : Controller
{
// GET: Explorer
public ActionResult Index()
{
return View();
}
// Example: https://localhost/Explorer/Import/id/200
[Route("Import/id/{importId:decimal}")]
public ActionResult Import(decimal? importId)
{
return View();
}
// Example: https://localhost/Explorer/Import/window/helloWorld
[Route("Import/{text}/{importWindow}")]
public ActionResult Import(string importWindow)
{
return View();
}
}
Define only the Default route in the RegisterRoutes() method:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}

Url.Action() does not use good route config

I have overridden a default route in my routes configuration :
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute("MyController_OverriddenAction",
"MyController/OverriddenAction",
new { controller = "MyOverriddenController", action = "OverridenAction" },
new[] { "Plugin" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "home", action = "Index", id = UrlParameter.Optional }
);
}
}
If I call MyController/OverriddenAction, the overridden action OverriddenAction in MyOverriddenController is displayed. It works.
But if I call #Html.Action("OverriddenAction", "MyController"), the default route is called.
Why? What is the solution?
My controllers :
public class MyOverridenController : Controller
{
public ActionResult OverriddenAction()
{
return Content("overridden");
}
}
public class MyController : Controller
{
public ActionResult OverriddenAction()
{
return new EmptyResult();
}
[...]
}
You should use following syntax
#Html.Action("OverriddenAction", "MyOverriden")
while calling the method directly like MyController/OverriddenAction it looks for the entry in MapRoute. However, while using #Html.Action you should use the actual controller name.

ASP.NET MVC URL Routing with ControllerName/ExampleID

i'm getting this error when i'm navigate browser to url:
localhost:10793/RealEstates/10
this my RouteConfig code:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Main", action = "Index" }
);
routes.MapRoute(
name: "RealEstates",
url: "RealEstates/{action}",
defaults: new { controller = "RealEstates", action = "Index" }
);
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
);
}
}
my error:
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
when changed code to:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}",
// defaults: new { controller = "Main", action = "Index" }
//);
//routes.MapRoute(
// name: "RealEstates",
// url: "RealEstates/{action}",
// defaults: new { controller = "RealEstates", action = "Index" }
//);
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id = UrlParameter.Optional }
);
}
}
it's work but when i call on other actions in controller
localhost:10793/RealEstates/CreateAd
this error found
The parameters dictionary contains a null entry for parameter 'id' of
non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult ViewAd(Int32)' in
'Youe3lan.Controllers.RealEstatesController'. An optional parameter
must be a reference type, a nullable type, or be declared as an
optional parameter.
Parameter name: parameters
and this my controller:
namespace MvcAppliction1.Controllers
{
public class RealEstatesController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult ViewAd(int id)
{
return View();
}
public ActionResult CreateAd()
{
return View();
}
}
}
You need to change it to:
routes.MapRoute(
name: "RealEstatesViewAd",
url: "RealEstates/{action}/{id}",
defaults: new { controller = "RealEstates", action = "ViewAd", id UrlParameter.Optional }
);}}
Have a look here it might help:
http://msdn.microsoft.com/en-us/library/cc668201(v=vs.100).aspx
UPDATE
Add this to your controller:
public ActionResult ViewAd(Int32 id)
{
return View();
}
You see
localhost:10793/RealEstates/10
is translated to:
localhost:10793/RealEstates/ViewAdd/10
So you need that method in the controller accepting an it parameter.
you've flagged your id in your route as optional:
id = UrlParameter.Optional
but I bet your controller isn't nullable??
public ActionResult ViewAd(Int32 id)
{
}
So you cant post a null into your id even though the route allows it. If you change this to:
public ActionResult ViewAd(Int32? id)
{
}
You won't get the error message:
The parameters dictionary contains a null entry for parameter 'id' of
non-nullable type 'System.Int32' for method
'System.Web.Mvc.ActionResult ViewAd(Int32)' in
'Youe3lan.Controllers.RealEstatesController'. An optional parameter
must be a reference type, a nullable type, or be declared as an
optional parameter. Parameter name: parameters

ASP NET Custom Routing

I have following patterns
/invitation/mission/busstop -- return the list of busstops
/invitation/mission/busstop/id -- return a specific busstop
/invitation/mission/driver -- return the list of drivers
/invitation/mission/driver/id -- return a specific driver
/invitation/mission/driver/city/model/limit -- query driver accoring to city, model and age limit
...
/invitation/questionair -- return the list of questionairs
/invitation/questionair/id -- return a specific questionair
/invitation/questionair/create -- create a new questionair
/invitation/questionair/update/id -- update a questionair
...
I expect the 'invitation' to be controller, and the rest to be action. Each of the above url should corresponds to a dedicated view page.
Can anyone help me to design the routes?
=====================================================================
I update the patterns, and add my expectation at the end of each url. Any suggest on the url patterns?
Here is the answer to keep your controller simple and still having good url patterns:
Controller:
public class InvitationController : Controller
{
public ActionResult GetAllBusStops()
{
//Logic to show all bus stops
//return bus stops
return View();
}
public ActionResult GetBusStopById(string id) //Assumed your id to be a string
{
//Logic to get specific bus stop
//return bus stop
return View();
}
public ActionResult GetAllDrivers()
{
//Logic for driver list
//return driver list
return View();
}
public ActionResult GetDriverById(int id) //Assumed your id to be an integer
{
//Logic to get specific driver
//return driver
return View();
}
public ActionResult GetDriver(string city, string model,int limit) //Assumed datatypes
{
//Logic to get specific driver with this criteria
//return driver
return View();
}
public ActionResult GetAllQuestionairs()
{
//Logic for questionair list
//return the list
return View();
}
public ActionResult GetQuestionairById(int id) //Assumed your id to be an integer
{
//Logic to get specific questionair
//return it
return View();
}
public ActionResult CreateQuestionair(QuestionairCreateModel model)
{
//logic to create questionair
return View();
}
public ActionResult GetQuestionairById(int id) //Assumed your id to be an integer
{
//Logic to get specific questionair
//return it
return View();
}
public ActionResult UpdateQuestionairById(int id) //Assumed your id to be an integer
{
//Logic to update specific questionair
//return it
return View();
}
}
Now go to your App_Start Folder, Open RouteConfig.cs, start adding the REST urls in the routes as below:
routes.MapRoute(
"List Bus Stops", // Route name
"invitation/mission/busstop", // No parameters for Getting bus stop list
new { controller = "Invitation", action = "GetAllBusStops"}, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get Bus stop by id", // Route name
"invitation/mission/busstop/{id}", // URL with parameters
new { controller = "Invitation", action = "GetBusStopById" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get All Drivers", // Route name
"invitation/mission/driver", // No parameters for Getting driver list
new { controller = "Invitation", action = "GetAllDrivers"}, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get Driver by id", // Route name
"invitation/mission/driver/{id}", // URL with parameters
new { controller = "Invitation", action = "GetDriverById" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get driver for city, model, limit", // Route name
"invitation/mission/driver/{city}}/{model}/{limit}", // URL with parameters
new { controller = "invitation", action = "GetDriver"}, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get All Questionairs", // Route name
"invitation/questionair", // No parameters for Getting questionair list
new { controller = "Invitation", action = "GetAllQuestionairs"}, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Get questionair by id", // Route name
"invitation/questionair/{id}", // URL with parameters
new { controller = "Invitation", action = "GetQuestionairById" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
"Create New Questionair, // Route name
"invitation/questionair/create", // URL with parameters
new { controller = "invitation", action = "CreateQuestionair" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"Update Questionair, // Route name
"invitation/questionair/update/{id}", // URL with parameters
new { controller = "invitation", action = "UpdateQuestionairById" }, // Parameter defaults
new { httpMethod = new HttpMethodConstraint("POST") }
);
Many things are assumed like the datatypes and model names. You can figure out how it works from this....
Add the route in the global.asax
routes.MapRoute(
"Default",
"{controller}/{id}",
new { controller = "invitation", action = "Index" }
);
Then in your controller use something like:
public class InvitationController : Controller
{
public ActionResult Index(string id)
{
switch(id.ToLower())
{
case "mission/busstop/list":
return View("busstop/list");
case "mission/driver/query":
return View("driver/query");
}
//Return default view
return View();
}
}
The other answers are valid, but I think there is a much easier, cleaner, and more maintainable way of defining your routes: use an attribute-based route mapper.
The easiest one I've found to use is the RiaLibrary.Web route mapper. All you have to do is add an attribute on each method you want to have a route for - then specify your route details.
To get set up, you must follow a few steps outlined on the RiaLibrary.Web page. After completing those steps, you can change any Controller action to look like this:
[Url("Articles/{id}")]
public ActionResult Details(int id)
{
// process
return View();
}
If you have any optional parameters, declare them as {param?} in the route string and then as int? param in your method declaration.
That's it! You can also include some other parameters to specify whether only certain types of HTTP calls match this route, to define constraints on parameters, and to fix the order in which this route is matched.

ASP.net mvc 2 routing

class HomeController
{
public ActionResult Search(int[] sections, int categories)
{
return View();
}
}
i need url like
website.com/search/1,2,3/5
What route map should i use?
At present RegisterRoutes looks
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
use something like:
var ints = new int[] {1, 2, 3, 4, 5};
var result = string.Join(",", ints.Select(x => x.ToString()).ToArray());
to build the commaseparated string, and pass it in the route "Search/{csv}/{somevalue}"
I'd change the controller definition to handle the array:
public ActionResult Search(string sections, int categories)
{
// sectionsArray is int[].
var sectionsArray = sections.Split(',').Select(x => int.Parse(x)).ToArray();
return View();
}
then you can define the routing as usual: Home/{sections}/{categories}:
routes.MapRoute(
"Search", // Route name
"{Home}/{sections}/{categories}", // URL with parameters
new { controller = "Home", action = "Search" } // Parameter defaults
);
Notice that you have to add this on top of the default one.
Add search route, don't replace default one
Since you'll probably still be using default route with controller/action combination I suggest you do this:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Search",
"search/{sections}/{section}", // rename these variables to what they actually are
new { controller = "Search", action = "Sections", sections = UrlParameter.Optional, section = UrlParameter.Optinal }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
But since it's hard to tell what requirements you're after here, maybe you're trying to use catch-all route parameter but not at the end of your URL request. If that's the case, you can check my code in blog post of such Route class that allows catch-all section anywhere in the URL and not just at the end as it is by default.

Resources