I am just puzzled on why is my RedirectToRoute() method not working. I have a RouteConfig.cs file like this
routes.MapRoute(
"pattern1",
"{action}",
new { controller = "Home", action = "About" }
);
routes.MapRoute(
"pattern2",
"{controller}/{action}/{id}",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
on this configuration my default controller Home and action About is getting called, now in the action method I am calling RedirectToRoute() with the following value like this
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
return RedirectToRoute("pattern2");
}
Why is the RedirectToRoute() not calling Admin/Index action
This is because of the route you have defined. RedirectToRoute() redirects to the route you have defined. The url you have defined for "pattern2" is "{controller}/{action}/{id}". If you want to use the overload which only accepts the routeName, then you need explicitly define the url in your RouteConfig. Example:
routes.MapRoute(
"pattern2",
"Admin/Index",
new { controller = "Admin", action = "Index", id = UrlParameter.Optional }
);
If you do not want to define the url explicitly, then you need to use a different overload of RedirectToRoute() which accepts the routeValues object.
Try this:
return RedirectToRoute(new
{
controller = "Admin",
action = "Index",
id = null
});
You could also use RedirectToAction() method. It seems more intuitive.
Related
I have Controller name: District and Action name: Incharges But I want the URL to be like this (action name with some paremeter)
www.example.com/district/incharges/aaa
www.example.com/district/incharges/bbb
www.example.com/district/incharges/ccc
But, while debugging teamName always return as NULL in the action parameter.
Routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"DistrictDetails",
"District/Incharges/{teamName}",
new { controller = "District", action = "Incharges" }
);
Controller
But, while debugging teamName always return as NULL in the action parameter.
public class DistrictController : Controller
{
public ActionResult Incharges(string teamName)
{
InchargePresentationVM INPVM = new InchargePresentationVM();
INPVM.InitializePath(teamName, string.Empty);
return View("", INPVM);
}
}
View
#{
ViewBag.Title = "Index";
}
<h2>Index About</h2>
specific route you have to declare the first
routes.MapRoute(
"DistrictDetails",
"District/Incharges/{teamName}",
new { controller = "District", action = "Incharges", id = UrlParameter.Optional }
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
););
ASP.NET MVC DefaultModelBinder will try and do implicit type conversion of the values from your value provider , eg. form, to the action method parameters. If it tries to convert a type from the value provider to the parameter and it is not able to do it, it will assign null to the parameter.
Regarding routing, ASP.NET MVC has the concept of conversion over configuration. If you follow the conversion, then instead of configuration. You can keep your default route and always have the route you want by naming your controllers, action methods and parameter names.
With the convention over configuration you must keep the default HomeController which is the entry point of the application and then name other controllers as below. These can conform to the route names you want.
namespace ConversionOverConfiguration
{
public class DistrictController: Controller
{
public ActionResult Incharges(int aaa)
{
//You implementation here
return View();
}
}
}
The route will look as below if you have this conversion implementation
//Controller/ActionMethod/ActionMethodParameter
//District/Incharges/aaa
And this will give you domain URI:www.example.com/district/incharges/aaa . If action method parameter type is a string, then domain URI is:www.example.com/district/incharges/?aaa=name
is a string. Then you can keep the ASP.NET MVC default routing
routes.MapRoute
(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional
});
I'm new to mvc. I'm creating a test application in mvc.
here I've studied that mvc works with url as /[Controller]/[ActionName]/[Parameters]
But in my application i have to pass parameter as /home/index?name=test. I think it should work as /home/index/test. But it doesn't work in this way.
Here is ActionMethod in homeController
public ActionResult Index(String name)
{
ViewBag.name = name;
return View();
}
Routing code in Global.asax.cs
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
);
}
Index.cshtml
#{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>#ViewBag.name</h2>
Can anyone help me to findout that why its not working in /home/index/test format.
Thanks.
Routes.MapRoute(
"DefaultWithName", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional }
Because your optional parameter says "id", and in your controller it's "name".
As Lars points out, your route specifies the default parameter name as ID. Your controller specifies it as "name." If you changed your controller parameter to say, int ID, then home/index/3 would work.
As pointed by #Lars & #Joel, your route specifies the default parameter name as ID.
Declare
routes.MapRoute(
"DefaultWithName", // Route name
"{controller}/{action}/{name}", // URL with parameters
new { controller = "Home", action = "Index", name = UrlParameter.Optional });
And to use route use code
#Url.RouteUrl("DefaultWithName", new { name = "test" })
Instead of #Url.Action
I have the following URl:
http://localhost:12981/BaseEvent/EventOverview/12?type=Film
This is route:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I want that in a browser the url looks like:
http://localhost:12981/Film/Overview/12
How can I do this?
One more example:
http://localhost:12981/BaseEvent/EventOverview/15?type=Sport
should be
http://localhost:12981/Sport/Overview/15
Thanks.
This should work:
routes.MapRoute("", "{type}/Overview/{id}", new { controller = "Events", action = "Overview");
Then you have a controller named EventsController with an action like this one
public ViewResult Overview(string type, int id)
{
//Your code
return View(model);
}
I have two actions in a controller and yet the parameters are not being passed into one of them.
This one: /RouteStop/List/1
And this one: /RouteStop/Details/100
And my global.asax:
routes.MapRoute(
"List",
"{controller}/{action}/{id}",
new { controller = "RouteStop", action = "List", id = UrlParameter.Optional }
);
routes.MapRoute(
"Details",
"{controller}/{action}/{routeID}",
new { controller = "RouteStop", action = "Details", routeID = UrlParameter.Optional }
);
And here's the actions from my Controller:
public ActionResult List(string id)
{
return View();
}
public ActionResult Details(string routeID)
{
return View();
}
When I access this URL (/RouteStop/Details/100) the parameter gets passed just fine. But when I access the other one (/RouteStop/List/1) the parameter is null. The names match up as they should but I can't figure it out.
Try replacing {controller} with List and Details in respective routes. but for your scenario the default routing that you get when you create an MVC app should work.
I have this URL:
/controller/action/value
and this action:
public ActionResult Get(string configName,string addParams)
{
}
How do I set up my routing table to get the routing engine bind the value to the configName parameter for any action in the Config controller?
Well, first off, that is incomplete. You don't have a method name.
Secondly, this will already work with URLs of the format:
/controller/action?configName=foo&addparams=bar
Here's how to do it with pretty routes:
routes.MapRoute(
"YourMapping",
"{controller}/{action}/{configName}/{addParams}");
or
routes.MapRoute(
"YourMapping",
"{controller}/{configName}/{addParams}",
new {
controller = "YourController",
action = "YourAction"
},
new {
controller = "YourController" // Constraint
});
if you want to exclude the action from the URL.
You could add a new route above the default
routes.MapRoute(
"Config",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Which will allow you to use the route /config/actionName/configName/addParamsValue. Your other routes should be unaffected by this.
routes.MapRoute(
"ValueMapping",
"config/{action}/{configName}/{addParams}",
new { controller = "Config", action = "Index", configName= UrlParameter.Optional, addParams = UrlParameter.Optional } // Parameter defaults);
Setting default Controller to Home, with a Default Action of Index
So the Url:
/config/get/configNameValue/AddParamValue
would match this Method:
public ActionResult Get(string configName,string addParams)
{
//Do Stuff
}