C# 将WebForm添加到MVC应用程序以使用无扩展的WebForm页面

C# 将WebForm添加到MVC应用程序以使用无扩展的WebForm页面,c#,asp.net,asp.net-mvc,webforms,C#,Asp.net,Asp.net Mvc,Webforms,我有一个MVC应用程序,其中我必须集成几个webforms页面 我只是简单地向root添加了一个webform webform.aspx,当我使用文件exetence访问webform时,它可以正常工作http://localhost:54363/WebForm.aspx 但是,当我尝试在没有文件扩展名的情况下访问它时,相同的文件不起作用 http://localhost:54363/WebForm 因为这是get 404错误 为此,我根据this对Global.asax文件进行了更改,但它不起

我有一个MVC应用程序,其中我必须集成几个webforms页面

我只是简单地向root添加了一个webform webform.aspx,当我使用文件exetence访问webform时,它可以正常工作http://localhost:54363/WebForm.aspx 但是,当我尝试在没有文件扩展名的情况下访问它时,相同的文件不起作用

http://localhost:54363/WebForm 因为这是get 404错误

为此,我根据this对Global.asax文件进行了更改,但它不起作用

下面是Global.asax文件的代码

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;

namespace ProjectNameSpace
{
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

        }


        public static void RegisterRoutes(RouteCollection routes)
        {

            routes.RouteExistingFiles = true;
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("Content/{*pathInfo}");
            routes.IgnoreRoute("Scripts/{*pathInfo}");
            routes.IgnoreRoute("{WebPage}.aspx/{*pathInfo}");
            routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");


            //routes.MapRoute(
            //    "Default", // Route name
            //    "{controller}/{action}/{id}", // URL with parameters
            //    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            //);

            routes.MapPageRoute("home", "WebForm/", "~/WebForm.aspx", false,
                new RouteValueDictionary {
                        { "path", "page-not-found" },{ "pagename", "page-not-found" }
                    });

        }
    }


}
以上代码中是否有错误,或者设置WebForm.aspx文件路由的正确方法是什么

更新:


我还通过在RouteConfig.cs文件中添加webform路由代码来解决这个问题

using AlhabtoorTennisAcademy.CustomFilters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace ProjectNameSpace
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            // webforms page route
            //Custom route code for webform
            routes.MapPageRoute("home", "WebForm", "~/WebForm.aspx", false,
              new RouteValueDictionary {
            { "path", "page-not-found" },{ "pagename", "page-not-found" }
            });
}
}
........

从我在上面的示例中看到的情况来看,您在使用MapRoute的默认MVC路由之后添加了MapPageRoute,因此MapPageRoute的顺序在MapRoute之后处理,这是错误的,因为路由的处理顺序是从最顶端到最底端,从最特定到最不特定

为了路由webforms页面,MapPageRoute必须位于MapRoute之前的最上面的顺序:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.RouteExistingFiles = true;
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.IgnoreRoute("Content/{*pathInfo}");
    routes.IgnoreRoute("Scripts/{*pathInfo}");
    routes.IgnoreRoute("{resource}.ashx/{*pathInfo}");

    // webforms page route
    routes.MapPageRoute("home", "WebForm", "~/WebForm.aspx", false,
        new RouteValueDictionary {
        { "path", "page-not-found" },{ "pagename", "page-not-found" }
    });

    // default MVC route
    routes.MapRoute(
       "Default", // Route name
       "{controller}/{action}/{id}", // URL with parameters
       new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
    );
}
补充说明:

您可以使用页面名称占位符在单个MapPageRoute定义中映射所有webforms页面:

routes.MapPageRoute("home", "{WebPage}", "~/{WebPage}.aspx");
相关问题:


不要删除默认的MVC路由,只需将其保留在最后一个订单上即可。我认为您应该启用一些命名的EnableFriendlyURL,并将MapPageRoute放在最上面。还要尝试检查/WebForm path采用了哪个路由。只是用代码尝试了一下,它不工作并返回HTTP错误404.0-找不到的错误http://localhost:54363/WebForm 当我使用.aspx时,它就工作了..我设法通过添加以下代码routes来解决这个问题.MapPageRoutehome,WebForm,~/WebForm.aspx,false,newrouteValueDictionary{{path,page not found},{pagename,page not found} }; 在RouteConfig.cs文件中