asp.net MVC generic action handler - c#

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.

Related

ASP.Net MVC3/4 multiple templates

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
);

MVC Routing: How to map two names to one action

I'd like to have one route that gives the option of two urls but maps to one action. A good example would be for multilingual application. Lets take english and french for example.
This seems simple at first, technically you can do:
routes.MapRoute(
"the hi route english" ,
"welcome/sayhi/{id}" ,
new { controller = "Welcome" , action = "SayHi" , id = UrlParameter.Optional }
);
routes.MapRoute(
"the hi route french" ,
"bienvenu/direallo/{id}" ,
new { controller = "Welcome" , action = "SayHi" , id = UrlParameter.Optional }
);
But that means that you'll have to define two routes for every action. Or a little better solution, create a custom Route class that takes more params to handle bilingualism.
If I go option 1 or 2, It means I have to define every single routes of the WelcomeController because I cannot use {action} in my route.
Ideally, i'd like to be able to define at least action name via metadata and then grab it via reflection or something.
i.e.:
[ActionName( { "fr", "direallo" }, {"en", "sayhi"})]
public ActionResult SayHi(string id){
//check current thread culture...
}
I am not quite sure where to starts, any ideas? Tips?
Thank you,
You have several options starting points here, roughly they are (in order of implementation complexity):
A route per language (as you outlined above)
A regex route constraint e.g.
routes.MapRoute(
"the hi route",
"{controllerName}/{actionName}/{id}",
new { controller = "Welcome" , action = "SayHi" , id = UrlParameter.Optional },
new { controllerName = #"welcome|bienvenu", actionName = #"sayhi|direallo" }
);
You could create a base controller, which is inherited by a subclass per language and define a language specific action name for each base controller action method
You could create your own (or use the one provided in the answer to the comment by Justin Pihony) custom routing constraint

How do I route a webforms page in ASP.NET MVC?

I have an MVC page that has a webforms page that it needs to render:
The virtual directory for the webforms page is:
http://mysite/Report/1
File saved:
~/Areas/Accounts/Views/Invoices/Report.aspx?id=1
How do I map this?
I have mapped it to controller:
return Redirect("~/Areas/Accounts/Views/Invoices/Report.aspx?id=1?id=" + id);
But I get an error.
You want to use the MapPageRoute() method to send something to a specific page:
routes.MapPageRoute(
"ReportRoute",
"Report/{id}",
"~/Areas/Accounts/Views/Invoices/Report.aspx?id={id}"
);
From the way you put it you might not be clear on what you're doing.
Add a Controller (from visual studio) in this folder : ~/Areas/Accounts/Controllers/Report
You would probably have a method display(int id) in your ReportController class. Then by default your URl will look like:
http://mysite/Report/display/1
To customize it you add this into Global.asax.cs :
routes.MapRoute(
"NewRoute", // Route name
"report/{id}", // URL with parameters
new { controller = "report", action = "display", // Parameter defaults
id = UrlParameter.Optional }
);

MVC Custom Routing

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.

MVC Routing problem

I am learning MVC and I need to understand why it doesn't work the way it should.
Here is my routing :
public static void RegisterRoutes(RouteCollection routes)
{
// Note: Change the URL to "{controller}.mvc/{action}/{id}" to enable
// automatic support on IIS6 and IIS7 classic mode
//http://localhost/store/category/subcategory/product
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Category", // Route name
"store/{category}/{subcategory}", // URL with parameters
new
{
controller = "Catalog",
action = "Index",
category = "Featured Items",
subcategory = "All Items"
}
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" }, // Parameter defaults
new { controller = #"[^\.]*" } // Parameter constraints
);
}
The way I understand routing I should see the following url when I start the web app :
http:/localhost/store/
What I get is the second route....
Furthermore if I change the second route to "home/{action}/{id} it doesn't catch any routes.
Could you help me understand this please..Thanks
Routes do not specify default URL; the default URL is handled by your app. Routing specifies that when it sees http://localhost/store/bikes/mountain, it will use the catalog controller. But that doesn't specify the default URL; you have to enter that in the project properties.
I would recommend not changing the second one because unless you are creating groupings for all of your controllers, it's best to have the default as it is so you can catch all URL's. Your change to the second one would require the url to be:
http://localhost/home/home/index to match the HomeController's index action, whereas the default setup catches http://localhost/home/index...
Does that make sense?
Try this: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx

Categories

Resources