I have this route map defined.
routes.MapRoute("default", // route name
"{controller}/{action}/{id}", // url with parameters
new { controller = "home", action = "index", id = UrlParameter.Optional }, // parameter defaults
new string[] { "mobilesurveys.mt.controllers" }
);
This will work perfectly. now I want to add another routemap
routes.MapRoute("couponreedem", // route name
"{controller}/{action}/{clientname}", // url with parameters
new { controller = "Rc", action = "index", id = UrlParameter.Optional }, // parameter defaults
new string[] { "mobilesurveys.mt.controllers" }
);
i have defined like this. Here Rc is my controller. and I am giving the url as
.com /Rc/Rc/sammy
and method in the controller defined as
public ActionResult Rc(string clientname)
{
viewModel =dataRc.ProductCategoryGet();
return View(viewModel);
}
clientname will be always null. How to add another route while the existing route not be disturbed.
Thanks.
It actually looks identical. But in case you want a new one you can try something like this, and it should be above the default one.
routes.MapRoute("couponreedem", // route name
"RC/{action}/{clientname}", // url with parameters
new { controller = "Rc", action = "index", clientname = UrlParameter.Optional }, // parameter defaults
new string[] { "mobilesurveys.mt.controllers" }
);
That will fix the route with RC/...
Also your action should be named Index
public ActionResult Index (string clientname)
{
viewModel =dataRc.ProductCategoryGet();
return View(viewModel);
}
Related
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
Hy
i had write below code
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Home", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"Controller_Action", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { id = UrlParameter.Optional } // Parameter defaults
);
foreach (var route in GetDefaultRoutes())
{
routes.Add(route);
}
routes.MapRoute(
"UserPage", // Route name
"{id}", // URL with parameters
new { controller = "Home", action = "Get" } // Parameter defaults
);
}
private static IEnumerable<Route> GetDefaultRoutes()
{
//My controllers assembly (can be get also by name)
Assembly assembly = typeof(test1.Controllers.HomeController).Assembly;
// get all the controllers that are public and not abstract
var types = assembly.GetTypes().Where(t => t.IsSubclassOf(typeof(Controller)) && t.IsPublic && !t.IsAbstract);
// run for each controller type
foreach (var type in types)
{
//Get the controller name - each controller should end with the word Controller
string controller = type.Name.Substring(0, type.Name.IndexOf("Controller"));
// create the default
RouteValueDictionary routeDictionary = new RouteValueDictionary
{
{"controller", controller}, // the controller name
{"action", "index"} // the default method
};
yield return new Route(controller, routeDictionary, new MvcRouteHandler());
}
}
i am new to mvc,i want to rewrite my url somthing like this,suppose my url is like www.myhouse.com/product/index/1 then i want to display only www.myhouse.com/prduct-name for better seo performance,i am using mvc4 beta,i had also one through URL Rewriting in .Net MVC but it is not working for me....
but i don't know how to pass pass value to this method.
After lots of searching on the internet, i got my solution
add below code to global.asax
routes.MapRoute(
"Home", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index" } // Parameter defaults
);
routes.MapRoute(
"jats", // Route name
"{jats}", // URL with parameters
new { controller = "Home", action = "Content" } // Parameter defaults
);
then add below code to view:
#Html.ActionLink("test", "Content", new { jats= "test-test" })
add below code to HomeController:
public ActionResult Content(string jats)
{
return View();
}
then you done...now URL is same as you pass in query string...so your controller name and query string parameter will not display.
Here is my .Net MVC Folder structure:
I have a separate Folder called “Area” and inside that I have a Folder called “Restaurant”.
Inside this “Restaurant” Folder I have a controller Called “MenuController” which has action named “Index”
I am tried to rewrite the (Custom Route Mapping) url inside “Global.asax.cs” using following code.
routes.MapRoute(
"RestaurantMenu", // Route name
"Restaurant/{id}", // URL with parameters
new { controller = "/Restaurant/Menu", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
But it gave me a HTTP 404 error.
The controller parameter inside your route should be the name of the controller, not the path :
If your controller name is Menu then, change it to this way :
routes.MapRoute(
"RestaurantMenu", // Route name
"Restaurant/{id}", // URL with parameters
new { controller = "Menu", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
And the another strange thing is that : is this route sitting inside your Global.asax file? It should be inside your RestaurantAreaRegistration.cs file as follows;
public class RestaurantAreaRegistration : AreaRegistration {
public override string AreaName {
get {
return "Restaurant";
}
}
public override void RegisterArea(AreaRegistrationContext context) {
context.MapRoute(
"RestaurantMenu", // Route name
"Restaurant/{id}", // URL with parameters
new { controller = "Menu", action = "Index", id = UrlParameter.Optional }
// Parameter defaults
);
context.MapRoute(
"Accommodation_default",
"accomm/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
then you can give this a shot. If you would like to use ActionLink with this route, use it like that :
#Html.ActionLink("MyLink", "Index", "Menu", new { id = 1, Area = "Restaurant"})
I have written the above code with notepad so there might be some typos :)
I'm kind of new to MVC. I have a controller called PostItemsController in an area called CaseManagers with an action method called GetByUmi(int caseNumber):
[HttpGet]
public ViewResult ViewByUmi(int umi)
{
//implementation omitted
}
The routing configuration looks like this (not my work):
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
//ignore route for ico files
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?../Images/MauriceFavicon.ico(/.*)?" });
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?Images/MauriceFavicon.ico(/.*)?" });
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?Content/Images/MauriceFavicon.ico(/.*)?" });
routes.IgnoreRoute("{*favicon}", new { favicon = #"(.*/)?/favicon.ico(/.*)?" });
//ignore javascript files routing
routes.IgnoreRoute("{file}.js");
//ignore routing for files ending .doc
routes.IgnoreRoute("{resource}.doc");
routes.MapRoute(
"CaseManagers", // Route name
"CaseManagers/PostItems/ViewByUmi/{id}", // URL with parameters
new { controller = "PostItems" } // Parameter defaults
);
//InvoicesLookUp route
routes.MapRoute(
"InvoicesLookUpShortDefault", // Route name
"InvoicesLookUp/{action}/{id}", // URL with parameters
new { controller = "InvoicesLookUp", action = "Index", area = "Home", id = UrlParameter.Optional } // Parameter defaults
,
null,
new[] { "MooseMvc.Areas.Accounts.Controllers" } // Parameter defaults
).DataTokens.Add("area", "Accounts");
//Invoices route
routes.MapRoute(
"InvoicesShortDefault", // Route name
"Invoices/{action}/{id}", // URL with parameters
new { controller = "Invoices", action = "Index", area = "Accounts", id = UrlParameter.Optional } // Parameter defaults
,
null,
new[] { "MooseMvc.Areas.Accounts.Controllers" } // Parameter defaults
).DataTokens.Add("area", "Accounts");
//administrating route
routes.MapRoute(
"AdministratorShortDefault", // Route name
"Administrator/{action}/{id}", // URL with parameters
new { controller = "Administrator", action = "Index", area = "Administrator", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
//add root route route
routes.MapRoute(
"Root",
"",
new { controller = "Home", action = "Index", id = "" }
);
When I try to call this method with the URL http://localhost:[portnumber]/CaseManagers/PostItems/ViewByUmi/1234 I get the following exception:
The parameters dictionary contains a null entry for parameter 'umi' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult ViewByUmi(Int32)' in 'MooseMvc.Areas.CaseManagers.Controllers.PostItemsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters
I don't intend the ID parameter to be optional and I don't understand why MVC can't find the ID.
Can anyone tell me what I need to do?
EDIT:
Phil Haack's route tester is telling me that the following route is being mapped:
routes.MapRoute(
"CaseManagers", // Route name
"CaseManagers/PostItems/ViewByUmi/{id}", // URL with parameters
new { controller = "PostItems" } // Parameter defaults
);
But it is being mapped AFTER another route CaseManagers/{controller}/{action}/{id}. But this route isn't anywhere in the Global.asax file (take a look, it's reproduced in full above).
Any idea what's going on?
Method parameters in ASP.NET MVC match up 1-1 with route parameters. Since you have no routes that take in a route value named umi, no route will catch what you're trying to do.
You have one of two choices:
If you want the default route to handle that action, then change:
public ViewResult ViewByUmi(int umi)
{
//implementation omitted
}
to:
public ViewResult ViewByUmi(int id)
{
//implementation omitted
}
However, if you want to keep umi(because it has contextual meaning that makes that code easier to follow), then you want to add a route to explicitly deal with it:
//UMI route
routes.MapRoute(
"umi",
"/case/postitems/view/{umi}",
new { area = "CaseManager", controller = "PostItems", action = "ViewByUmi", umi = "" }
);
Turns out that Global.asax isn't the only place that routing happens. Each of the areas in this application has its AreaRegistration class. I added a new route to the top of this class to produce the following:
public class CaseManagersAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "CaseManagers";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"PostItems", // Route name
"CaseManagers/PostItems/ViewByUmi/{umi}", // URL with parameters
new { area = "CaseManagers", controller = "PostItems", action = "GetByUmi", umi = "{umi}" } // Parameter defaults
);
context.MapRoute(
"CaseManagers_default",
"CaseManagers/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
}
The routing debugger now tells me this is getting matched first. Now I just need to work out why I've got an error telling the the resource cannot be found...
You don't have a route for CaseManagers/PostItems/ViewByUmi/1234 and it would appear that it is taking ViewByUmi and try to convert it to an System.Int32 because it is falling into the Default route. If you create a Route for your CaseManagers you should no longer have this problem.
Use Phil Haacks' Route Debugger to help you out :o)
routes.MapRoute(
"CaseManagers", // Route name
"CaseManagers/PostItems/ViewByUmi/{id}", // URL with parameters
new { controller = "PostItems" } // Parameter defaults
);
I am new to ASP.Net MVC. May be this question looks simple, but i couldn't fix it. Here the scenario. I have an application listing data based on city. So the url will be looking like this
www.xxxxxx.in/chennai
www.xxxxxx.in/mumbai
www.xxxxxx.in/delhi
In normal routing the first part (chennai/mumbai) is controller in the above url, But here i dont want this to be a controller. instead i want to map the single controller (LocationController) to these URl's. Because later time i can add any number of city.
I am struck here, can someone help me out.
Try this:
routes.MapRoute(
"CityRoute", // Route name
"{city}", // URL with parameters
new { controller = "Location", action = "Index", city = "" } // Parameter defaults
);
I am not sure there won't be easier option than this, but you can try this - using route constraint. Basically, you need to know the list of cities you have and then constrain the route to match only entries in that list.
The route constraint can be implemented as follows
public class CityConstraint : IRouteConstraint
{
public static IList<string> CityNames = (Container.ResolveShared<ICityService>()).GetCities();
bool _IsCity;
public CityConstraint(bool IsCity)
{
_IsCity = IsCity;
}
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
if (_IsCity)
return CityNames.Contains(values[parameterName].ToString().ToLower());
else
return !CityNames.Contains(values[parameterName].ToString().ToLower());
}
}
And then put the route as follows:
routes.MapRoute("Location", "{cityName}", new { controller = "LocationController", action = "Index" }, new { cityName = new CityConstraint(true) });
Also make sure the above route is listed before the default route
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional
);
Also note that, no controller name can be a city name.
Try this and see.
If all your routing is related to these cities than remove default route and replace it with this route definition:
routes.MapRoute(
"Default",
"{city}",
new { controller = "Location", action = "Index", city = "Mumbai" }
);
Then create a LocationController class:
public class LocationController : Controller
{
public ActionResult Index(string city)
{
// do whatever needed; "city" param has the city specified in URL route
}
}
If you still need your default route (controller/action/id) for other pages not just cities then it's probably better to put a constraint on your default route and define them like this:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "Home|...|..." } // put all controllers here except "Location"
);
routes.MapRoute(
"Location",
"{city}",
new { controller = "Location", action = "Index", city = "Mumbai" }
);
This will make other controllers still working and location will work just as well. The problem is of course if there's a city name that's the same as a name of one of your regular controllers. :) But you can control/avoid that as well.
You can do that by adding a route that hardcodes the controller name:
routes.MapRoute(
"location", // Route name
"{cityName}", // URL with parameters
new { controller = "location", action = "index" } // Parameter defaults
);
routes.MapRoute(
"Location", // Route name
"{controller}/{action}/{cityName}", // URL with parameters
new { controller = "Location", action = "index"} // Parameter defaults
)
This will route all requests of the form "/mumbai" to LocationController action method Index with parameter cityName set to "mumbai". It will also be able to route full controller/action spec using the second route.