【发布时间】: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