Route from Incoming ASPX url to an ASP.NET MVC Controller Action - asp.net-mvc-3

I have a url Review.aspx?reviewId=3 and I'd like to have this url be routed to an MVC controller/action Review/3. Any ideas?

Never mind, a simple route like this worked:
routes.MapRoute(
"Reviews_Old", // Route name
"LOreview.aspx", // URL with parameters
new { controller = "LOReview", action = "Review", id = UrlParameter.Optional } // Parameter defaults
);
And the query string parameters are model bound on the controller action parameters

Related

MVC3 :: Using global.asax to direct a specific controller/action to a different controller/action

In an MVC3 app I have a view in my home controller that is accessed via the following URL:
http://mysite.com/home/mycontent
However, my user wants to accees it via:
http://mysite.com/mycontent (without a specific reference to the home controller)
I've tried modifying my global.asax with the following:
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
But I get 404 errors when I try to use it. Is there a way I can accomplish what my user is wanting to do - redirecting a URL to a specific controller/view pair?
I think you should keep some "parameters":
routes.MapRoute(
"MyContent", // Route name
"{action}/{id}", // URL with parameters
new { controller = "Home", action = "MyContent", id = ""} // Parameter defaults
);
In what order are your routes registered? That route should come before your default route.
E.g.
routes.MapRoute(
"MyContent", // Route name
"MyContent", // URL with parameters
new { controller = "Home", action = "MyContent"} // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Pages", action = "Index", id = UrlParameter.Optional }
);
Otherwise, it should work... I have routes defined exactly the same as that.

mvc3 IModelBinder and url

I'm having a problem using iModelBinder with url in the format of
http://localhost/controller/action/id/value
the action would be the function in the controller
the id/value is ie. id=12
When I try the above link i receive a 404 error page not found, and looking at the stack I can understand that MVC is looking for a path it does not understand.
using the following works
http://localhost/controller/action?id=value
If anyone as any idea if this problem can be resolved, I would really like to be able to use "/" as separators.
Vince
The url should really be in the format:
http://localhost/controller/action/id
For example:
http://localhost/products/index/1
And the id should then be specified in the controller action. For example:
public ActionResult Index(int id)
{
...
The route specified in the global.asax file will specify the format of the url. For the above url the default route will suffice:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
Then the default model binder will automatically bind your id (i.e. 1 in the above url) to the int id in the action.
Like Adam was suggesting, I don't think you should specify the name of the id in the url as it is automatically bound to for you by the default model binder.

Incorrect Routing is ASP.NET MVC

I don't know why I have such problems with ASP.NET MVC routing. I wish there was a tool that showed me which routes I had currently setup. Regardless,
In my global.asax.cs file I have the following:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"SignUp", // Route name
"account/{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Register" } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
}
I have the following defined in HomeController.cs
public ActionResult Register()
{
return View();
}
I was expecting to be able to access this page by visiting /account/register in my browser. However, I continue to get a 404. What am I doing wrong?
/Account/Register matches your first route.
The word Register is matched to the {controller}, so it looks for a controller named RegisterController.
replace
routes.MapRoute(
"SignUp", // Route name
"account/{controller}/{action}", // URL with parameters
new { controller = "Home", action = "Register" } // Parameter defaults
);
with
routes.MapRoute(
"SignUp", // Route name
"account/{action}", // URL with parameters
new { controller = "Home" } // Parameter defaults
);
This will mean /account/register will route to the Register action on the Home controller. It will also mean that action links and other links you generate via #Html.ActionLink("Register", "Register", "Home") will generate the URL /account/register
Think of the 'URL with paramters' as a pattern that the URL will be matched against.
The problem with your original route map is that it is looking for a URL like this /account/controllername/actionname. So, when you go /account/register - it is taking register as the controller name, and taking the default action name (in this case register) - and as the 'register' action does not exist in the 'register' controller - you are getting a 404.
UPDATED
I updated my suggested route as per Robert's comments.
It is also worth noting, as Robert states, that this whole thing could be made more simple by making a 'Account' controller, and moving the 'Register ' action there. Then you could delete the 'SignUp' route, and just use default routing. If you thought about it, you'd agree that this would be a better place for a 'Register' action than the 'Home' controller.
Try using this nugget package http://nuget.org/packages/Glimpse.Mvc3
You can find more info about glimpse on http://getglimpse.com/

MVC 3 Custom Url Routing

I want to create a custom route, as a default mvc creates route like this:
domain.com/deals/detail/5
but in my case i want to create a custom route for it:
domain.com/delicious-food-in-paris
so it has to look for deals controller's detail action with passing an id value 5 to it.
how can i do it?
Thanks
This route maps all one segment urls to the detail method of the deals controller and passes one string argument called dealName to it:
routes.MapRoute(
null,
"{dealName}",
new { controller = "deals", action = "detail" }
);
But as AdamD have said, you should register that route as the last route in your setup because it will catch all urls which have only one segment.
With this approach you have to lookup your deal by name which might be not acceptable. So many apps use a hybrid approach and include the name and the id in the url like this:
domain.com/deals/5-HereComesTheLongName
Then you can use a route like this to get the id and optionally the name:
routes.MapRoute(
null,
"{id}-{dealName}",
new {
controller = "deals",
action = "detail",
dealName = UrlParameter.Optional
}
);
You can do this by defining a custom route in the Global.asax RegisterRoutes function. You would need to add this route after the default route so if the default controller action id pattern fails it will try to execute the final route.
An example would be to use the following:
routes.MapRoute(
"RouteName",
"/{uri}", //this is www.domain.com/{uri}
new { controller = "Controller", action = "ByUri" },
new { uri = #"[a-z\-0-9]+" } //parameter regex can be tailored here
);
After you register this route you can add a new custom controller to handle these routes, add the action that will handle this and accept a string as the parameter.
In this new action you can have a database lookup or some hardcoded valide routes that return Views.

Make three-part route url for controller in ASP.NET MVC3

I am using ASP.NET MVC3 and curious about how to make routes like /Account/Ajax/Action map to controller AccountAjaxController method Action
Is there any way to do so?
Already solved issue using following code in Global.asax.cs file:
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new {controller = "Home", action = "Index", id = UrlParameter.Optional},
new[] {"MyProjectName.FrontEnd.Web.Logic.Controllers"}
);
routes.MapRoute(
"Ajax",
"Ajax/{controller}/{action}/{id}",
new[] {"MyProjectName.FrontEnd.Web.Logic.Controllers.Ajax"}
);
One way that I've done this is with RouteCollection.MapRoute(). See http://msdn.microsoft.com/en-us/library/dd470521.aspx. You can do all kinds of stuff with routes.
In the code below I would typically call this from my global.asax.cs Application_Start():
RouteCollection routes = RouteTable.Routes;
routes.MapRoute(
"ArbitraryRouteName", // Route name
"Account/Ajax/Action/", // URL with parameters
new
{
controller = "AccountAjax",
action = "Action"
}, // Parameter defaults
new
{
}
);
There are many ways to use this in order to generate links. You can use Html.RouteLink() in your views as an example. I believe that you can also call Html.ActionLink("link text", "AccountAjax", "Action"). I think that providing there is only one Route mapped to your controller and action name, ASP.NET MVC automatically figures out the proper URL to generate from your route mappings.
See http://msdn.microsoft.com/en-us/library/dd492585.aspx for an example of the RouteLink extension method.

Resources