I have set up two custom extensions to enable MVC in IIS6.
So the site can be accessed with a URL of either something like...
mysite/mycontroller.europe/myaction
or like...
mysite/mycontroller.america/myaction
What is the most robust way of finding the extension from the RequestContext instance?
So I would like to be able to write something like...
var location = reqContext.......GetExtenstion(); // location = "europe"
and obviously have that work even if the setup of the site/directories changes a little.
Define a route:
routes.MapRoute(
"DefaultWithExtension",
"{controller}.{extension}/{action}",
new { controller = "Home", action = "Index", extension = "america" }
);
and then:
var extension = RequestContext.RouteData.GetRequiredString("extension");
Also you can just define extension as a string parameter for all relevant actions for the controllers, in which case it will be directly available.
e.g.
public ActionResult myaction(string extension)
This does still need the mapRoute entry defined above.
Related
I am making a documentation for the OSS I am about to publish. The url is going to be like /Documentation/{class name} . All the documentation view are named as {namespace}_{classname}. Basically i wonder if there is a way to direct all requests to /Documentation/* to a method inside my Documentation controller so that i can do something like
return View({class name});
instead of having to make a method for each class
You can handle that in routing.
routes.MapRoute(
"Docs", // Route name
"Documentation/{className}", // URL with parameters
new { controller = "Documentation", action = "Show", className = "" } // Parameter defaults
);
That assumes of course that you have a DocumentationController with a Show attribute.
There is any way to change the defualt views path in MVC3/4. i.e: The Url http://localhost:000/Home (the controller Home) will represent the view at Views/Style1/Home/Action.
Thanks ahead!
Ok, now that I understand the question better after the edit, I think this is what you're looking for:
You can change the ViewLocation in Application_Start().
The example below assumes use of the Razor View Engine.
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine { ViewLocationFormats = new string[] { "~/Views/Style1/{1}/{0}.cshtml" } } );
Answer was partially derived and referenced from this post
You should be able to set the Default route for your application to use a different base path. You can typically set the route in the Global.asax in the RegisterRoutes method.
Example:
routes.MapRoute(
"Default", // Route name
"Style1/{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have the following route:
routes.MapRoute(
"Default", // Route name
"{language}/{controller}/{action}/{id}",
new { language = "en", controller = "XY", id = UrlParameter.Optional }
);
For changing the language of the site, I would like to create links, which correspond exactly to the url of the current page, but have another language part.
What would be the best way, to generate such a link?
Most likely, you'll want to insert the {language} value as part of each link-rendering View's ViewModel or ViewData. There's a few different way you could achieve this; such as overloading your controller's OnActionExecuted() method. If you take this approach, I suggest making all of your controllers inherit from a single base controller.
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
filterContext.Controller.ViewData["Language"] = GetUserLanguage();
base.OnActionExecuted(filterContext);
}
And then in your View:
#Url.Action("Index", "Home", new { language = ViewData["Language"] })
Alternatively, you might consider overloading the Url.Action() and Html.ActionLink() methods with logic which appends this value automatically.
You could do something like this:
#Html.RouteLink("Switch Language", "Default", new {
controller = ViewContext.RouteData.Values["controller"],
action = ViewContext.RouteData.Values["action"],
id = ViewContext.RouteData.Values["id"],
language = "fr"
})
It would probably make sense to make an extension method to handle this though.
Say I have set up a url structure as follows (ASP.NET MVC2)
http://localhost:XXXX/Product/
Click on link browse by color
http://localhost:XXXX/Product/Color/
Click on link browse red color items by type (i.e. pen's)
http://localhost:XXXX/Product/Color/Red/Pen
In the controller, I will need to do a select based on these criteria. Except when previously, I could go
public ActionResult ShowTypesForColor(string color)
but to do this one:
public ActionResult ShowItems(string type)
I also need the color that was selected.
How could I do this? Is splitting up the url string the only way?
edit: maybe i've gotten ahead of myself in the global.asax.cs
routes.MapRoute(null, "Product/Color/", new { controller = "Product", action = "ShowAllColors" });
routes.MapRoute(null, "Product/Color/{color}", new { controller = "Product", action = "ShowTypesForColor" });
routes.MapRoute(null, "Product/Color/{color}/{type}", new { controller = "Product", action = "ShowDetail" });
I don't think I can define the last one like that can I? with two {} values?
Your last route seems perfectly valid. It will map to action with signature like this:
ActionResult ShowDetails(string color, string type) {
return View(/*view params*/);
}
EDIT I think the order is wrong, so if the last route is not being fired, try doing this:
routes.MapRoute(null, "Product/Color/{color}/{type}", new { controller = "Product", action = "ShowDetail" });
routes.MapRoute(null, "Product/Color/{color}", new { controller = "Product", action = "ShowTypesForColor" });
routes.MapRoute(null, "Product/Color/", new { controller = "Product", action = "ShowAllColors" });
The order of MVC routes should be from most specific to least specific, otherwise the least specific route (/product/color/{color}) will match a url product/color/red/pen before the more specific /product/color/{color}/{type}
You can put multiple tokens in your route (e.g., {color} and {type}) but it's not going the work the way you have it there. Why have you defined "Color" as the second segment of your URL? Why not just do /Products/Red and /Products/Red/Pen? It's inconsistent to do .../Colors/Red and not .../Types/Pen so I'd just leave the "Colors" and "Types" qualifiers out altogether.
I'd define your ShowItems() method like this:
public ActionResult ShowItems(string color, string type)
this will allow you to have a route like /Products/Red/Pen where your route maps to this ShowItems() method. But you'll still need to differentiate that from the ShowTypesForColor() method where it also takes a first parameter of color. The routing framework will just treat type as null - for the route that has both tokens, make sure you have a route constraint specifying that neither color nor type can be null/empty (i.e., for the ShowItems() route).
I'm new to MVC. I'm having trouble trying to accomplish having a route setup in the following manner:
System/{systemName}/{action}
Where systemName is dynamic and does not have a "static" method that it calls. i.e.
http://sitename.com/Systems/LivingRoom/View
I want the above URL to call a method such as,
public void RouteSystem(string systemName, string action)
{
// perform redirection here.
}
Anyone know how to accomplish this?
routes.MapRoute(
"Systems_Default",
"System/{systemName}/{action}",
new { controller="System", action = "RouteSystem", systemName="" }
);
Should route your request as you specified.
Note that with the above route, your Url should be:
http://sitename.com/System/LivingRoom/View
I had a similar problem. I used the following route.
routes.MapRoute(
"BlogSpecific", // Route name
"{blogSubFolder}/{controller}/{action}/{id}", // URL with parameters
new { blogSubFolder = "", controller = "", action = "", id = "" } // Parameter defaults
);
blogSubFolder just gets passed to my controller actions as a parameter. Controller and action all work just as they usually do. Just swap out blogSubfolder with your "System" paramter and that will hopefully work for you.
It looks like you intend to route to a controller based on the system name. If that is correct then you would simply need this:
routes.MapRoute("Systems",
"Systems/{controller}/{action}"
new{controller = "YourDEFAULTController", action = "YourDEFAULTAction"});
Please note that the third line only sets the default values specified if they are NOT included in the url.
Given the route above, MVC would route this:
http://sitename.com/Systems/LivingRoom/View
to the View action method on the LivingRoom controller. However this:
http://sitename.com/Systems/LivingRoom
would route to the YourDEFAULTAction method on the LivingRoom controller.