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

后端 未结 4 1445
陌清茗
陌清茗 2020-12-01 10:02

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 brows

相关标签:
4条回答
  • 2020-12-01 10:23

    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)

    0 讨论(0)
  • 2020-12-01 10:24

    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() } }
    );
    
    0 讨论(0)
  • 2020-12-01 10:31

    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.

    0 讨论(0)
  • 2020-12-01 10:32

    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); 
    
    0 讨论(0)
提交回复
热议问题