parameters in url in asp.net mvc - c#

I know this can be done, but don't even know what its called to find a good tutorial from Google. I am using ASP.Net MVC4 and I have a controller called ePage, right now I can access what I want from a URL like this
http://www.myUrl.com/ePage/{ACTION}/{PARAMETER as "id"}
how can I change the routing so that (just for this controller if possible) it is read like this
http://www.myUrl.com/ePage/{PARAMETER}
I will always be using "Index" as Action for now.
If there is a simple answer to do that'd be awesome , if not just a point to the right direction for me to read and figure out.

In your Global.asax.cs under the RegisterRoutes method, you can try adding:
routes.MapRoute("MyNewRoute", "ePage/{param}", new {
controller = "ePage",
action = "Index",
});
Your Index method must have an argument named param so that the routing will match.

Related

Override webforms with MVC routing

I am working on an existing ASP.NET webforms project - slowly updating to MVC. I am trying to conditionally override some of the existing webforms pages by routing to new MVC controllers. I can easily route an "example.aspx" URL to an MVC controller, but it doesn't work if there is an existing "example.aspx" file.
Example code:
routes.MapRoute
(
name: "example",
url: "example.aspx",
defaults: new
{
controller = "Example",
action = "Index"
}
constraints: new { useMvc= "1" }
);
The code above works fine, and routes to the controller as expected, unless there is an existing "example.aspx" file in my solution - in which case it routes to that instead. But that's exactly what I'm trying to override.
Are existing webform routes given preferential treatment? Is there some way to circumvent this?
I've found mechanisms to do the reverse by using MapPageRoute(), but as far as I can see, that doesn't help me.
You can configure your application to ignore specific routes that your are handling through MVC. You could add the following line before registering a new route:
routes.IgnoreRoute("{*path}/example.aspx");
Hope this helps.
I haven't solved the problem as stated, but I've found a work-around that will accomplish the same result.
1) I renamed the "example.aspx" file to something else "foo.aspx"
2) Added the MapRoute() similar to my original question (with the constraint)
3) Used MapPageRoute() to route all other "example.aspx" calls to "foo.aspx"
I also ended up having to use the QueryStringConstraint logic as described by
another stackoverflow answer: but that's a separate issue.
I'll leave this question open for a few more days to see if there is a "real" solution before marking my own answer.

Call MVC method from Controller Full Path

I have an issue because I do not know how to call a specific method inside a Controller that is far away on the foler structure. I try to call it this way from my View
#Html.Action("Method", "DesiredControllerName", parameter)
I am pretty sure this works, because I am calling other methods from controllers this way. The only problem is that this time I need to reach a controller that is way up in a different folder, i.e.
The controller is in:
MySolution/Areas/Area1/Controllers/DesiredControllerName
Whereas the CurrentView Im making the call is in:
MySolution/Areas/Area2/Views/Client/CurrentView
How can I make the call work?
Thanks!
EDIT:
I found it,
As the controller is in another Area I just had to add it as a parameter
The controller for path was not found or does not implement IController
looks like you maybe got it, where your example code line:
#Html.Action("Method", "DesiredControllerName", parameter)
has the 'parameter' parameter, try using an array of anonymously typed routevalues and include an area and controller, something like this:
#Html.Action("Method", new { Area = "Area1", Controller =
"DesiredControllerName", })
I am used to using this style of providing mvc routing info using Html.ActionLink, but the Html.Action should be similar-ish. here is the msdn page for the Action method, hope that helps
#Html.Action("Method", "DesiredControllerName", new { Area = "Area1" })
This will works

How to ignore all characters after "controller/action" in an ASP.NET MVC route?

I would like my ASP.NET MVC4 application to only serve the base HTML markup for a specific page, and after that I'm processing everything else on client-side with knockout.js/history.js/AJAX, including the initial page load.
So when someone refers to URL http://example.com/products/list/food/fruits, the MVC router should simply ignore everything what is behind "products/list" and route the request to ProductsController and List action. Then on client-side I will handle the rest and load the requested data accordingly.
I was playing with the route definitions, I tried to completely skip the "products/list" route, I also tried to add a "products/list/*" route, but didn't have success yet.
You can use an asterisk as part of the last variable in a route. For example, when configuring your routes:
routes.MapRoute(
"ProductRoute",
"products/list/{*otherArgs}",
new { controller = "Products", action = "List" });
You can learn more in MSDN's Documentation on routing under the section "Handling a Variable Number of Segments in a URL Pattern"
You will need to create your own route.
Something like this should do the trick:
routes.MapRoute("Products", "Products/{List}",
new {controller = "Products", action = "List"}
);
Note: I´m not sure if the other parameters are required in the route.

MVC 4 edit route parameter in controller

I'm having a little problem i hope someone can help me with.
on ASP.net MVC 4 (C#) i need to be able to edit the parameters of my route from my controller.
example the request url is
MyController/MyAction/param1/param2
now from MyAction I need to edit the returned url so that it displays
MyController/MyAction/Modifiedparam1/Modifiedparam2
The purpose of this is to translate the parameters that i retrieve from my database from language changes.
Please use redirect result for this (in MyAction on some condition):
return RedirectToAction("MyAction", { param1Name = Modifiedparam1, param2Name = Modifiedparam2 };
Basically you cannot modify url in controller. Urls is something send by the browser to invoke some action. You may just say browser to redirect user to another url.
Don't fight with framework. It will fight back sooner or later. Instead follow mvc principles, redirect from controller action or filters/interceptors to do so.

Asp.net MVC2 URL structure - best practices

What are the best practices for setting up projects with multiple interfaces in asp.net MVC2? I'm starting a project where we need a typical arrangement like:
example.com/ - end-user interface
example.com/Admin/ - site management
The site will be involved enough that simply having all the user logic in HomeController and all the admin logic in AdminController isn't really an option. I've read about using multiple Route definitions, but the following doesn't seem to work properly:
routes.MapRoute(
"Admin", // Route name
"Admin/{controller}/{action}/{id}", // URL with parameters
new { controller = "Admin", 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
);
It causes all the links on the homepage to point to example.com/Admin/local-part instead of example.com/local-part.
I'd like to avoid specifying a different MapRoute for each Admin controller (omitting {controller} from the mapping each time).
I've also read about setting up different Areas within a project, but that seems like it would be too involved for the scope of this project.
Finally, I've also read that you can put constraints on MapRoutes, but the documentation on that part seems confusing to me. Is there something obvious I'm missing? Failing that, are there any best practices for asp.net structure that I should keep in mind?
It sounds like Area's is ready made for what you want to do. Setting up an area isn't really all that involved, basically you just have to register it. By default the area routing will match the default in the global.asax, the exception being the extra "\area" slug in the url. I'm fairly certain it only took me a few minutes when I set this up on a project a few months ago.
If your Admin controller is complicated enough to exceed the scope of a single controller then that indicates that an area may be warranted.

Categories