How to route a .aspx page in asp.net mvc 3 project?
Solution 1
Solved! So, we need to add a route contraint to the webforms route to ensure that it only catches on incoming routes, not outgoing route generation.
Add the following class to your project (either in a new file or the bottom of global.asax.cs):
public class MyCustomConstaint : IRouteConstraint{
public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection){
return routeDirection == RouteDirection.IncomingRequest;
}
}
Then change the Tickets route to the following:
routes.MapPageRoute(
"Tickets",
"Reports/Tickets",
"~/WebForms/Reports/Tickets.aspx",
true, null,
new RouteValueDictionary { { "outgoing", new MyCustomConstaint() } }
);
Solution 2
If you are trying to utilise web forms in a MVC project then I would move your .aspx out of the views folder, as it isn't really a view, so something like WebForms/Tickets/Report.aspx.
In web forms you map a route by calling the MapPageRoute
method.
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Tickets", "Reports/Tickets", "~/WebForms/Tickets/Report.aspx");
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
You'll need to put that before the default MVC route.
Vinicius Ottoni
Updated on April 17, 2020Comments
-
Vinicius Ottoni about 4 years
I have a .aspx page in the following path:
Areas/Management/Views/Ticket/Report.aspx
I want to route that to the following path in my browser:
http://localhost/Reports/Tickets
How can i do that?
I try this:
routes.MapRoute( "Tickets", // Route name "Areas/Management/Views/Ticket/Report.aspx", // Original URL new { controller = "Reports", action = "Tickets" } // New URL );
But i got the
404
error.What i'm doing wrong?
Obs: I put that before the
Default
route. -
Vinicius Ottoni about 12 yearsIf i put the
Reports/Tickets
how the site 'll route to my aspx page? -
Parv Sharma about 12 yearsull have to make a route handler.. currently ur url is getting mapped to Reports/Tickets which obsly dosent exists.. and hence ur getting 404. try returning a string from "Reports/Tickets" and ull see where u r wrong
-
Vinicius Ottoni about 12 yearsDude, i don't want to see that i'm wrong. "I'm wrong", ok, i know (it's because i'm here ^^), now i have to know how to solve my problem. =) A code, an exemple, a link...
-
Vinicius Ottoni about 12 yearsBut with this the url (when i return the .aspx page) will have the .aspx at the end, will not?
-
coffeeyesplease about 12 yearsNo it will not. Have you actually tested it, (voting me down)? I've actually corrected the code so it will return localhost/Reports/Tickets
-
coffeeyesplease about 12 yearsif you trying to use crystal reports then you should check this answer stackoverflow.com/questions/348785/…
-
Vinicius Ottoni about 12 yearsWhen i run the project the browser redirect to the folling url "localhost:37538/Reports/…". Oo
-
Vinicius Ottoni about 12 yearsI saw it. The page is working (i've tested in another project), my problem now is only route it in asp.net mvc project.
-
Vinicius Ottoni about 12 yearsWhat i have to put in
defaults
? And innew CustomRouteHandler()
you want to meannew ASPXRouteHandler()
, right? -
coffeeyesplease about 12 yearsIs your problem really in MVC, from what I've gathered from your previous comments you have webform doing the display, so perhaps what you need is the IIS url rewrite module to take of this specific page. learn.iis.net/page.aspx/461/… or do you have a controller to handle the request before sending to your view?
-
Vinicius Ottoni about 12 yearsWhen i run the project the browser redirect to the folling url "localhost:37538/Reports/…". Oo
-
Parv Sharma about 12 yearsdo u have some kind of authentication in place.. like forms authentication?
-
Chris Diver about 12 yearsIt's matching the default route at a guess, the line above needs to be directly after the
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
line -
Vinicius Ottoni about 12 yearsI put that directly after the
routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
and i got the same url. =/ -
Vinicius Ottoni about 12 yearsI cannot use this. The prerequisite is "IIS 7 with ASP.NET role service enabled." and my IIS is 6. =/
-
Chris Diver about 12 yearsI've just tried it in a new mvc project and that works fine for me. Are you putting
/Reports/Tickets
directly into your browser or clicking a link on another page? -
Vinicius Ottoni about 12 yearsI'm running the project, and the browser redirects to this url. Oo
-
Parv Sharma about 12 yearsk then if u can authticate and then url this Url - "Reports/TIcket" it would be awsm.. becuz ur authenticated then u wont be redirected to the login url or ull have to add the Url Reports/TIcket to unauthenticated access by using webconfigs location tag. search it.
-
coffeeyesplease about 12 yearsHow about this: hanselman.com/blog/…
-
Vinicius Ottoni about 12 yearsPut in you project a forms auth i see if when you try to access the root page, it's redirected to this url (block in web.config all paths if the user is not logged).
-
Vinicius Ottoni about 12 yearsDude, i didn't understand what you said. What is "awsm"? And to you know, this page "Reports/TIcket" could only be access if the user is logged.
-
Chris Diver about 12 yearsI get redirected to
http://localhost/Account/LogOn?ReturnUrl=%2f
- I suspect there is in either your routing or web.config that's causing this. Maybe try it in a clean MVC project. Also this answer may help. stackoverflow.com/a/2747571/385149 -
Vinicius Ottoni about 12 yearsI cannot to use as pdf only. I need to use the ReportViewer.
-
Vinicius Ottoni about 12 yearsI debug the project, and i saw that after add the
MapPageRoute
, the project stops to enter in myHome Controller
. -
Vinicius Ottoni about 12 yearsWhen you run the project you're aready logged in? If yes, try to log off and you will see (i test in a separeted project too).
-
Chris Diver about 12 yearsNo, if I was already logged in then i wouldn't get redirected to the Account/LogOn page.
-
JabberwockyDecompiler about 11 yearsThank you CodeHobo for this solution. Reference
-
Anup Sharma about 9 yearsThank you for your suggestion.
-
Vinicius Ottoni over 6 yearsI'm Vecthor. @JabberwockyDecompiler
-
JabberwockyDecompiler over 6 yearsHmm, not sure why I put that, did you change your name?
-
Vinicius Ottoni over 6 yearsI made the same question there, so, with the answer of CodeHobo I put here. "I'm Vecthor" there. It is only a alias, codename, something like that ^^.
-
JabberwockyDecompiler over 6 yearsMy friends call me Jabber :D, haha, actually I go by TJ.