I have a few controllers ,
One let say computer controller
It has action for laptop, desktop gadgets
i would like to have pages name :www.MyDomain/laptop (and so on )
and one let say electronic controller
it has action TV, DVD, (and so on )
i would like to have pages name :www.MyDomain/TV (and so on )
Without specifies the controller?
I don’t understand what happen to my question before I can't edit
(I hope the admin will delete it )
You could do this by specifying route constraints:
routes.MapRoute(
"Computers",
"{name}",
new { controller = "Computers", action = "Index", name = UrlParameter.Optional },
new { page = "laptop|desktop" }
);
routes.MapRoute(
"Gadgets",
"{name}",
new { controller = "Electronic", action = "Index", name = UrlParameter.Optional },
new { page = "tv|dvd" }
);
Now /laptop and /desktop will be routed to the Index action of the ComputersController and /tv and /dvd will be routed to the Index action of the GadgetsController.
In your Global.asax.cs:
routes.MapRoute(
"ViewLaptop", // Route name
"/laptop", // URL with parameters
new { controller = "Computers", action = "Laptop" } // Parameter defaults
);
This should do it.
Related
Orchard CMS in MVC3 Application.
How to remove the unwanted "url" content?
Example: http://www.xxxxxx.com/HotelsOnly/HotelList/Region?region=2114&total=848
In routes {area,” HotelsOnly”} ,{controller,”HotelList”}
Url How to change or remove this(/HotelsOnly/HotelList)
Example: http://www.xxxxxx.com/Region?region=2114&total=848
Explain how to remove? Please show any Example.
I think you use this type of rout url it's blow
routes.MapRoute(
"Regis", // Route nameRegister
"Test/Artical/Show/{id}", // URL with parameters
new { controller = "Artical", action = "Show", id = UrlParameter.Optional }
in my project i use rout like this it's below
#Html.RouteLink("click", "Regis", 1);
1 i set a default value for example . so my url look like this
http://localhost:xxxx/Test/Artical/Show/1
I remove Test from my url like this it's below
you will change your rout like this
routes.MapRoute(
"Regis", // Route nameRegister
"Test/Artical/Show/{id}", // URL with parameters
new { controller = "Artical", action = "Show", id = UrlParameter.Optional }
);
and after change rout then my url look like this
http://localhost:xxxx/Artical/Show/1
i think this will help you
new RouteDescriptor {
Route = new Route(
"Region",
new RouteValueDictionary {
{"area", "HotelsOnly"},
{"controller", "HotelList"},
{"action", "Index"}
},
new RouteValueDictionary(),
new RouteValueDictionary {
{"area", "HotelsOnly"}
},
new MvcRouteHandler())
},
I have been trying to learn ASP.NET MVC 3 and things are going well apart from the routing aspect, whatever I try I just can't seem to get them quite right.
I have an ActionLink on the main page:
#Html.ActionLink("Contracts", "List", "Contract",
new { User.Identity.Name, page=1 })
Which is meant to access this method in the ContractController:
public ViewResult List(string user, int page = 1)
{
//snip
}
My routes are:
routes.MapRoute(
null,
"Page{page}",
new { Controller = "Contract", action = "List" }
);
routes.MapRoute(
null,
"Page{page}",
new { Controller = "Contract", action = "List", user = "", page = 1 }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
The link now will return a 404 error as it can't find the action 'List' in the controller 'Home', which obviously means it didn't use either of the first routes.
Everything worked before I tried to add parameters to the ActionLink, so basically, what am I doing wrong?
Thanks very much.
Alex,
You're doing all the other bits absolutely correctly, however the actionlink has a missing parameter, try this for your actionlink:
#Html.ActionLink("Contracts", "List", "Contract",
new { User.Identity.Name, page = 1 }, null)
Adding the null as the final param (htmlAttributes) is all that's missing for you in this scenario (there are 9 overloads for Html.ActionLink, so it's VERY easy to miss the correct implementation).
I am trying to get my routes setup for an Area and I have the following URLs that I need to allow:
http://localhost/Review/Setup
http://localhost/Review/Setup/65
http://localhost/Review/Setup/_AjaxGetMember?ReviewId=53
Area = Review | Controller = Setup | Action = Index
Thus for the URLS, they should go to:
http://localhost/Review/Setup/Index
http://localhost/Review/Setup/Index/65
http://localhost/Review/Setup/_AjaxGetMember?ReviewId=53
Here is the route that is currently registered for the area.
context.MapRoute(
"Review_default",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
}
);
Right now, 1 and 3 in my first list work but 2 does not without putting Index into the URL. What else can I add to make these URLs work so that Index does not have to be added or show up in the URL?
Thank you.
I did not test it but that's how I would try it:
context.MapRoute(
"Review_setup_directId",
"Review/Setup/{id}",
new
{
controller = "Setup",
action = "Index",
id = UrlParameter.Optional
}
);
Solved it. I needed to add more routes. There may be a more elegant way to do this but I just tested and it works.
context.MapRoute(
"Review_default",
"Review/{controller}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
id = #"\d*"
}
);
context.MapRoute(
"Review_default2",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
action = #"[A-Za-z]+",
id = #"\d*"
}
);
context.MapRoute(
"Review_default3",
"Review/{controller}/{action}/{id}",
new
{
action = "Index",
id = UrlParameter.Optional
},
new
{
action = #"_[A-Za-z]+",
id = #"\d*"
}
);
Super simple MVC site with an Area to handle mobile devices. All of my Area routing works fine with the exception of a view that expects a parameter.
In the "normal" site I have a view video page that expects a parameter.
mysite.com/Video/123456
This works perfectly. After fighting this for a bit in my Area for the mobile content I have even gone down to using the exact same code/markup in my Controller and View. So I would expect that the following URL:
mysite.com/Mobile/Video/123456
Would resolve properly. It doesn't. I get a 404 (not found). If I take the parameter off:
mysite.com/Mobile/Video
It resolves properly.
I am sure this must be something I am doing wrong in the routing. Below is the appropriate section from my global.asax. Any help would be appreciated.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
routes.MapRoute(
"NewsItem", // Route name
"NewsItem/{id}", // URL with parameters
new { controller = "NewsItem", action = "Index", 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
new string[] { "mysite.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { area = "Mobile", controller = "Home", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.HomeController" }
);
routes.MapRoute(
"Mobile/Video", // Route name
"Mobile/Video/{id}", // URL with parameters
new { area = "Mobile", controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Areas.Mobile.Controllers.VideoController" }
);
}
SteveInTN, you cannot have the same registration in both, Global.asax and MobileAreaRegistration.cs.
You only need to have Mobile Registration on MobileAreaRegistration.cs and call AreaRegistration.RegisterAllAreas() in Application_Start before RegisterRoutes(RouteTable.Routes).
If you want url like mysite.com/Mobile/Video/123456:
The mobile route registration should be in the format {controller} / {id}, like video route.
Registration in Global.asax:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Video", // Route name
"Video/{id}", // URL with parameters
new { controller = "Video", action = "Index", id = UrlParameter.Optional }, // Parameter defaults
new string[] { "mysite.Controllers.VideoController" }
);
//newsitem route
}
Registration on MobileAreaRegistration:
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"Mobile_default",
"Mobile/{controller}/{id}",
new { action = "Index", id = UrlParameter.Optional }
);
}
Looks like your Route name should not contain / since it may conflict with routing? When I do routing I make sure the names are unique and use underscores to represent separators like so : text_text. Not sure if this will work, worth a try though.
I am trying to create routes which can apply 1 and 2 type of URLs.
1 - First route will be at the start of application and I want 2 type of URLs that can used to access index page. I cannot hit below route when I have URL with Home at the end instead going to type 2.
http://www.example.com Or http://www.example.com/Home
routes.MapRoute(
"Default",
"{controller}/{action}",
new { controller = "Home", action = "Index", name = "" }
);
2 - This type of URL is passing "Name" parameter to load contents from DB. I want this URL like
http://www.example.com/Page?name=Contact Or
http://www.example.com/Page?name=Contact&id=22
But I want above URL like
http://www.example.com/Contact Or http://www.example.com/About
Or
http://www.example.com/Contact/22 Or http://www.example.com/About/33
Where
Contact and About are values for "Name" parameter passed in URL. Below is the Route used
routes.MapRoute(
"DynamicPages",
"{name}",
new { controller = "Home", action = "Page" }
);
Here is a possible solution. I am not sure if this is the right way to do this.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//To match http://www.mysite.com
routes.MapRoute(
"RootUrl",
"",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional }
);
//To match http://www.mysite.com/Home
routes.MapRoute(
"RootUrlWithAction",
"Home/{action}",
new { controller = "Home", action = "Index" }
);
//To match http://www.mysite.com/Contact Or
// http://www.mysite.com/About Or
// http://www.mysite.com/Contact/22 Or
// http://www.mysite.com/About/33
routes.MapRoute(
"DynamicPages",
"{name}/{id}",
new { controller = "Home", action = "Page",
id = UrlParameter.Optional }
);
// Everything else
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index",
id = UrlParameter.Optional } // Parameter defaults
);
}
Tested the following routes. Here the site root url is http://localhost:5879/. Refer the output screenshots provided below for each of the below mentioned scenario.
http://localhost:5879/ --> Uses first route map
http://localhost:5879/Home --> Uses second route map
http://localhost:5879/Contact --> Uses third route map
http://localhost:5879/About/33 --> Uses third route map
http://localhost:5879/Home/Page?name=Contact&id=22 --> Uses third route map
http://localhost:5879/Home/Index/2 --> Uses fourth route map
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Hope that gives you some idea to solve your issue.