16

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
  • 4,631
  • 9
  • 42
  • 64
  • Is your .aspx page, a web forms aspx page or an mvc webforms view engine page? – Chris Diver Apr 16 '12 at 13:44
  • You can have .aspx views which use the web forms view engine, and you can have .aspx web forms. I suspect you are trying to get a hybrid of web forms and mvc to work, but it is unclear (to me at least) which you are trying to achieve. – Chris Diver Apr 16 '12 at 13:51
  • I'm trying to use a crystal reports in my asp.net mvc project. And i'm trying to use a ReportViewer that only works (with full features) in web forms. So, now i only want to route the .aspx page that has the ReportViewer as mvc views (without .aspx at the end of URL). – Vinicius Ottoni Apr 16 '12 at 13:56

4 Answers4

22

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.

Chris Diver
  • 19,362
  • 4
  • 47
  • 58
  • When i run the project the browser redirect to the folling url "http://localhost:37538/Reports/Tickets?action=Index&controller=Login". Oo – Vinicius Ottoni Apr 16 '12 at 14:19
  • It's matching the default route at a guess, the line above needs to be directly after the `routes.IgnoreRoute("{resource}.axd/{*pathInfo}")` line – Chris Diver Apr 16 '12 at 14:28
  • I put that directly after the `routes.IgnoreRoute("{resource}.axd/{*pathInfo}")` and i got the same url. =/ – Vinicius Ottoni Apr 16 '12 at 14:31
  • 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? – Chris Diver Apr 16 '12 at 14:35
  • I'm running the project, and the browser redirects to this url. Oo – Vinicius Ottoni Apr 16 '12 at 14:38
  • 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 Apr 16 '12 at 14:41
  • 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. http://stackoverflow.com/a/2747571/385149 – Chris Diver Apr 16 '12 at 14:53
  • I cannot to use as pdf only. I need to use the ReportViewer. – Vinicius Ottoni Apr 16 '12 at 16:34
  • I debug the project, and i saw that after add the `MapPageRoute`, the project stops to enter in my `Home Controller`. – Vinicius Ottoni Apr 16 '12 at 16:39
  • 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). – Vinicius Ottoni Apr 16 '12 at 17:08
  • No, if I was already logged in then i wouldn't get redirected to the Account/LogOn page. – Chris Diver Apr 16 '12 at 17:51
14

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() } }
);
Vinicius Ottoni
  • 4,631
  • 9
  • 42
  • 64
-1

you are doing it opposite. this maps your url Areas/Management/Views/Ticket/Report.aspx to { controller = "Reports", action = "Tickets" }
what u should do instead is set the url as
Reports/Tickets EDIT:- you can create a routeHandler just for routing to this .aspx page.. like this.

public class ASPXRouteHandler : IRouteHandler 
{ 
   public IHttpHandler GetHttpHandler(RequestContext requestContext) 
   { 

     return BuildManager.CreateInstanceFromVirtualPath("~/Areas/Management/Views/Ticket/Report.aspx",  typeof(Page)) as Page; 
   } 
}

then u can add ur route to the existing routes table using

Route customRoute = new Route("Reports/Ticket",null, new ASPXRouteHandler()); 
      routes.Add(customRoute); 
Parv Sharma
  • 12,581
  • 4
  • 48
  • 80
  • If i put the `Reports/Tickets` how the site 'll route to my aspx page? – Vinicius Ottoni Apr 16 '12 at 13:46
  • 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 – Parv Sharma Apr 16 '12 at 13:48
  • 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 Apr 16 '12 at 13:51
  • What i have to put in `defaults`? And in `new CustomRouteHandler()` you want to mean `new ASPXRouteHandler()`, right? – Vinicius Ottoni Apr 16 '12 at 14:23
  • When i run the project the browser redirect to the folling url "http://localhost:37538/Reports/Tickets?action=Index&controller=Login". Oo – Vinicius Ottoni Apr 16 '12 at 14:28
  • do u have some kind of authentication in place.. like forms authentication? – Parv Sharma Apr 16 '12 at 14:28
  • 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. – Parv Sharma Apr 16 '12 at 14:39
  • 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. – Vinicius Ottoni Apr 16 '12 at 14:44
-2

if you leave the default routing when you create the asp.net project

public class ReportsController : Controller
{
        public ActionResult Ticket()
        {
            return View();
        }        
}

this should do the trick. The routing in asp.net mvc means that you don't link directly to .aspx but to Actions (methods) that in turn return an appropriate view (.aspx)

coffeeyesplease
  • 1,018
  • 9
  • 15
  • But with this the url (when i return the .aspx page) will have the .aspx at the end, will not? – Vinicius Ottoni Apr 16 '12 at 13:57
  • No it will not. Have you actually tested it, (voting me down)? I've actually corrected the code so it will return http://localhost/Reports/Tickets – coffeeyesplease Apr 16 '12 at 14:00
  • if you trying to use crystal reports then you should check this answer http://stackoverflow.com/questions/348785/crystal-reports-in-asp-net-mvc – coffeeyesplease Apr 16 '12 at 14:17
  • 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 Apr 16 '12 at 14:20
  • 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. http://learn.iis.net/page.aspx/461/creating-rewrite-rules-for-the-url-rewrite-module/ or do you have a controller to handle the request before sending to your view? – coffeeyesplease Apr 16 '12 at 14:28
  • I cannot use this. The prerequisite is "IIS 7 with ASP.NET role service enabled." and my IIS is 6. =/ – Vinicius Ottoni Apr 16 '12 at 14:33
  • How about this: http://www.hanselman.com/blog/PlugInHybridsASPNETWebFormsAndASPMVCAndASPNETDynamicDataSideBySide.aspx – coffeeyesplease Apr 16 '12 at 14:40