How to route a .aspx page in asp.net mvc 3 project?

28,587

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.

Share:
28,587
Vinicius Ottoni
Author by

Vinicius Ottoni

Updated on April 17, 2020

Comments

  • Vinicius Ottoni
    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
    Vinicius Ottoni about 12 years
    If i put the Reports/Tickets how the site 'll route to my aspx page?
  • Parv Sharma
    Parv Sharma about 12 years
    ull 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
    Vinicius Ottoni about 12 years
    Dude, 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
    Vinicius Ottoni about 12 years
    But with this the url (when i return the .aspx page) will have the .aspx at the end, will not?
  • coffeeyesplease
    coffeeyesplease about 12 years
    No 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
    coffeeyesplease about 12 years
    if you trying to use crystal reports then you should check this answer stackoverflow.com/questions/348785/…
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    When i run the project the browser redirect to the folling url "localhost:37538/Reports/…". Oo
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    I 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
    Vinicius Ottoni about 12 years
    What i have to put in defaults? And in new CustomRouteHandler() you want to mean new ASPXRouteHandler(), right?
  • coffeeyesplease
    coffeeyesplease about 12 years
    Is 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
    Vinicius Ottoni about 12 years
    When i run the project the browser redirect to the folling url "localhost:37538/Reports/…". Oo
  • Parv Sharma
    Parv Sharma about 12 years
    do u have some kind of authentication in place.. like forms authentication?
  • Chris Diver
    Chris Diver about 12 years
    It'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
    Vinicius Ottoni about 12 years
    I put that directly after the routes.IgnoreRoute("{resource}.axd/{*pathInfo}") and i got the same url. =/
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    I cannot use this. The prerequisite is "IIS 7 with ASP.NET role service enabled." and my IIS is 6. =/
  • Chris Diver
    Chris Diver about 12 years
    I'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
    Vinicius Ottoni about 12 years
    I'm running the project, and the browser redirects to this url. Oo
  • Parv Sharma
    Parv Sharma about 12 years
    k 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
    coffeeyesplease about 12 years
    How about this: hanselman.com/blog/…
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    Put 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
    Vinicius Ottoni about 12 years
    Dude, 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
    Chris Diver about 12 years
    I 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
    Vinicius Ottoni about 12 years
    I cannot to use as pdf only. I need to use the ReportViewer.
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    I debug the project, and i saw that after add the MapPageRoute, the project stops to enter in my Home Controller.
  • Vinicius Ottoni
    Vinicius Ottoni about 12 years
    When 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
    Chris Diver about 12 years
    No, if I was already logged in then i wouldn't get redirected to the Account/LogOn page.
  • JabberwockyDecompiler
    JabberwockyDecompiler about 11 years
    Thank you CodeHobo for this solution. Reference
  • Anup Sharma
    Anup Sharma about 9 years
    Thank you for your suggestion.
  • Vinicius Ottoni
    Vinicius Ottoni over 6 years
    I'm Vecthor. @JabberwockyDecompiler
  • JabberwockyDecompiler
    JabberwockyDecompiler over 6 years
    Hmm, not sure why I put that, did you change your name?
  • Vinicius Ottoni
    Vinicius Ottoni over 6 years
    I 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
    JabberwockyDecompiler over 6 years
    My friends call me Jabber :D, haha, actually I go by TJ.