【问题标题】:Adding WebForm to MVC application to work with extension less webform page将 WebForm 添加到 MVC 应用程序以使用无扩展的 webform 页面
【发布时间】:2019-06-09 02:21:27
【问题描述】:

我有一个 MVC 应用程序,我必须在其中集成几个 Web 表单页面。

我只是在根目录下添加了一个网络表单“WebForm.aspx”,当我使用文件扩展名http://localhost:54363/WebForm.aspx 访问网络表单时它可以正常工作,但是当我尝试在没有文件扩展名.aspx 的情况下访问它时,相同的文件不起作用

http://localhost:54363/WebForm 获取404 error

为此,我根据 article 对 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" }
            });
}
}
........

【问题讨论】:

  • 不要删除默认的 MVC 路由,只需将其保留在最后一个顺序上即可。我认为您应该启用命名为EnableFriendlyUrls() 并将MapPageRoute 放在首位。
  • 还可以尝试RouteDebugger 来检查/WebForm 路径所走的路线。

标签: c# asp.net asp.net-mvc webforms


【解决方案1】:

从我在上面的示例中看到的,您在使用MapRoute 的默认MVC 路由之后添加MapPageRoute,因此MapPageRoute 的顺序在MapRoute 之后处理,这是错误的,因为处理了路由从最高到最低的顺序(从最具体到最不具体)。

为了路由网络表单页面,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 定义中的所有 Web 表单页面:

routes.MapPageRoute("home", "{WebPage}", "~/{WebPage}.aspx");

相关问题:

URL Routing C# mvc and Web Forms

【讨论】:

  • 刚刚用代码试了一下,它不起作用并为http://localhost:54363/WebForm返回HTTP Error 404.0 - Not Found错误,当我使用.aspx时它就起作用了..
  • 我设法通过在 RouteConfig.cs 文件中添加以下代码 routes.MapPageRoute("home", "WebForm", "~/WebForm.aspx", false, new RouteValueDictionary { { "path", "page-not-found" },{ "pagename", "page-not-found" } }); 来解决这个问题
猜你喜欢
  • 1970-01-01
  • 2013-12-09
  • 2011-05-24
  • 1970-01-01
  • 2021-07-08
  • 1970-01-01
  • 2010-11-23
  • 1970-01-01
  • 2016-07-30
相关资源
最近更新 更多