Route request for Home.aspx to a controller - c#

This has likely been answered (perhaps multiple times) but I can't quite seem to figure it out from any of the posts I'm finding.
Our old website had a Home.aspx and there are still links out there that point to this page. So in our MVC site I want a route that will take incoming requests for Home.aspx and route it to a controller.
From what I can tell MapPageRoute seems to do the opposite by mapping a route to a page. How do I route a request for a physical page to a controller though?

This should map the route however you want it to:
routes.MapRoute(
name: "SomeName",
url: "Home.aspx",
defaults: new { controller = "Controller", action = "Action", id = UrlParameter.Optional }
);

Related

Html.ActionLink behind-the-scene logic in asp.net mvc [duplicate]

I have an existing Web application that was developed in ASP.NET 4.0. I want to add MVC functionality to the app, so I've integrated MVC into the app as per Scott Hanselman's article Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications. Because MVC routing is greedy, I added the following code to my Global.asa so that an empty URL will go to my Default.aspx:
routes.MapPageRoute("WebFormsDefault", "", "~/Default.aspx");
The problem now is that ActionLinks and RouteLinks don't form correctly. If I try to create an action link using:
#Html.ActionLink("Item List Page", "List", "Item")
the following URL is created:
"/SiteName/?action=List&controller=Item
I've found several posts from others with this same problem, but none of them have any answer. Is this just a bug? Is integrating MVC into a WebForms app just a bad idea in general? Or is there a way to fix this so that my Default.aspx page will be displayed when a user first enters the site and ActionLinks and RouteLinks will work correctly?
Coming to this a bit late, but I figure better late than never. I was having this exact same issue and I solved it by grouping my MapPageRoute code and my MapRoute code and then always calling the MapRoute code first. Example:
Originally my routing looked like this -
routes.MapPageRoute("401", "401/", "~/Views/Error/401.aspx");
routes.MapPageRoute("404", "404/", "~/Views/Error/404.aspx");
routes.MapPageRoute("500", "500/", "~/Views/Error/500.aspx");
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
etc etc
This was causing all of my form actions to direct to a url formatted as so:
/mysite/401?action=x&controller=y
Clearly that was not useful. By making sure that I always setup all of my MVC routes first, the problem resolved itself. I ended up making two seperate methods, one for configuring MVC routes and one for configuring Webform routes as so:
RouteConfig.RegisterMvcRoutes(RouteTable.Routes); // contains only MapRoute
RouteConfig.RegisterWFRoutes(RouteTable.Routes); // contains only MapPageRoute
(these calls go into the Global.asax file as usual and replace RouteConfig.RegisterRoutes)
I would not advise mixing webforms with MVC, but I did manage to get this working by using this helpful posting:
http://bartwullems.blogspot.com/2011/04/combining-aspnet-webforms-and-aspnet.html
I also had to be rather careful about the ordering of the routes so that my most generic route went after the aspx page I wanted to be served up as a default.
Here is my complete RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute({resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "SignUp",
url: "SignUp",
defaults: new { controller = "Profile", action = "SignUp", shortUrl = UrlParameter.Optional });
routes.MapRoute(
name: "Admin",
url: "Admin",
defaults: new { controller = "Admin", action = "Index", shortUrl = UrlParameter.Optional });
//used to get aspx page to render
routes.MapPageRoute("WebForms", "", "~/WebForms/Default.aspx", false, null, new RouteValueDictionary(new { controller = new IncomingRequestConstraint() }));
//this generic route must go last
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" }
);
}
public class IncomingRequestConstraint : IRouteConstraint
{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
{
return routeDirection == RouteDirection.IncomingRequest;
}
}

Routes are simultaneously valid, yet broken

I asked a previous question here, in which I attempted to use a blank URL to catch a default page.
After some more digging, and some trial and error, I stumbled upon the use of {*url} to catch the root URL. I also attempted to use a constraint to manage the "tidy" url that I want to use. My RouteConfig now looks like so:
routes.MapRoute(
name: "LoginRoute",
url: "{login}",
defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional },
constraints: new { login = "login" }
);
routes.MapRoute(
name: "Default",
url: "{*url}",
defaults: new { controller = "authController", action = "routingsuccess" }
);
However, neither of these routes result in a web page. Instead, they still result in 404. Curiously, however, Phil Haack's RouteDebugger reports that the URL I am using is valid, as demonstrated here:
To clarify, accessing the root url (in this case, localhost:3000) results in the same issue.
There is a valid controller, and a valid view behind it with the appropriate action. What could possibly be wrong?
You can just use the normal default routing.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "authController", action = "Index", id = UrlParameter.Optional }
);
This just mean, if a controller isn't passed in the url then it will use MainController. If an action isn't passed then it will use Index. This mean http://website.com will go to MainController action Index.
Maybe you are using [Authorise] attribute, or something else for authentication.
I will just make a guess : -
Your route is registred, and there is no problem in it. Problem occurs when you try to access it. There might be some kind of authorisation, like an [Authorise] attribute, that would block non access users to get to your route.
Or there might be something else, that would be causing your code to not be able to reach the ActionResult.
To confirm this, put a breakpoint in the constructor of the COntroller.Remove all attributes from the COntroller. If your debugger stops at the breakpoint in constructor, then the issue is not is registering the route, but access related.
Let me know if it helps.

rewriting urls for simple html pages in asp.net MVC app

I have asp.net mvc application.
And I have html page. called lp.html.
i want to rewrite page name url from lp.html to something else, lets say appndomainname.com/location
how can I do this, because in this case I am dealing with simple html pages. No controller or controller method.
So code like this won't be needed
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
any help will be appreciated, thanks

MVC4 MapRoute for CMS

Am building a CMS using MVC4. My experience with MVC is limited and trying to create a MapRoute that can handle the page structured created by the CMS. URLs for the pages would be along the lines of website.com/About
To handle this I have come up with the following
routes.MapRoute(
name: "Default",
url: "{p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
This works fine for root level pages but if I want sub pages like website.com/About/OurTeam
I get a 404. Ideally what I would like is just be able pass either the whole url after the .com bit as a string and sort it out in the controller or to split the levels up as an array of parameters and pass that through as 'p'.
Hope that makes sense. Any ideas?
You can use:
routes.MapRoute(
name: "Default",
url: "{*p}",
defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
);
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.
You could also add a route constraint to this route which can determine whether the page exists in the database or something. See this post for more info.

Why does my route get redirected, this doesn't make any sense to me

I have these 2 routes mapped out:
routes.MapRoute(
"Admin",
"admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "index", id = "" }
);
and then I have:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
So the 2 routes are identical, except the first one has /admin prefixed in the URLS.
This is what is happening, I have no idea how to explain this:
When I go to:
www.example.com/user/verify
it redirects to
www.example.com/admin/user/complete
instead of
www.example.com/user/complete
The action Verify simply redirects to Complete like this:
return RedirectToAction("complete", "user");
And all the complete action does is populate the ViewModel, and then calls the view.
How can it be redirecting and adding the prefix /admin/ to the URL?
I believe it is redirecting to the Admin route because the Admin route is the first with all the matching parameters (controller and action in the case provided). If you want to use something like this you will need to either look into using areas (MVC2) or using a named route redirect.
admin is your controller, you dont need an admin/controller/action the default route works just fine
all you need is an admin controller and the default route will find it for you
ie {controller}/{action}/{id}
will send /admin/addproduct to a controller named admin and an action called addproduct
you only need to add routes if you want something custom for example
/products/televisions/hdtv/2
where products would be a controller and the last 3 are category,subcategory and pagenumber
on the controller you point it to within your route.
hope that makes sense
Not sure exactly how your controllers are structured, but you can add a constraint to the first MapRoute to limit it to the specific controllers you want the route to apply to:
routes.MapRoute(
"Admin",
"admin/{controller}/{action}/{id}",
new { controller = "Admin", action = "index", id = "" } ,
new { controller = "[Some regex Expression - e.g. Admin]" }
);
Which will make the route only applicable for those controllers related routes. You can also use this tool to debug your routes. Depends how you have things structured, but like #NickLarson said - sounds like your using area functionality of MVC 2.
mvc goes from top to bottom while matching router, that' why you are dealing with this problem

Categories