Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 407
Vinicius Ottoni Avatar asked Apr 16 '12 13:04

Vinicius Ottoni


People also ask

Which method is used to map the route to an ASPX webpage?

In ASP.NET Webform application, request handler is . aspx file, and in MVC, it is the Controller class and Action method. For example, http://domain/students can be mapped to http://domain/studentsinfo.aspx in ASP.NET Webforms, and the same URL can be mapped to Student Controller and Index action method in MVC.

Which method is used for adding routes to an MVC application?

When an MVC application first starts, the Application_Start() method is called. This method, in turn, calls the RegisterRoutes() method. The RegisterRoutes() method creates the route table. The default route table contains a single route (named Default).

Where are routes registred in ASP.NET MVC application?

This route pattern is registered via call to the MapRoute() extension method of RouteCollection. When the MVC application launches the Application_Start() event handler of the global. asax execute that call the RegisterRoutes() method from RouteConfig class under App_Start directory (App_Start/RouteConfig.


2 Answers

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.

like image 122
Chris Diver Avatar answered Oct 24 '22 15:10

Chris Diver


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() } }
);
like image 32
Vinicius Ottoni Avatar answered Oct 24 '22 15:10

Vinicius Ottoni