【问题标题】:Newly created WebForm in folder is not found error未找到文件夹中新创建的 WebForm 错误
【发布时间】:2019-06-12 06:35:44
【问题描述】:

我正在 ASP.NET MVC 中创建一个项目,其想法是创建独立视图,为此我创建了一个公共文件夹,并在其中创建了一个 WebForm.aspx 及其自己的代码隐藏 .aspx.cs。我已经从 NuGet 打包器安装了 Bootstrap,将脚本引用到 WebForm 中,如下所示:

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="../../Content/bootstrap.min.css" rel="stylesheet" />
        <script src="../../Scripts/bootstrap.min.js"></script>
        <script src="../../Scripts/jquery-3.3.1.min.js"></script>
        <script src="../../Scripts/popper.min.js"></script>
    </head>
    <body>
        <form id="form1" runat="server">
            <div class="container-fluid">
                <h1>My First Bootstrap Page</h1>
                <p>This is some text.</p>
            </div>
        </form>
    </body>
    </html>

但是当我运行它时它会显示一个错误:

这是结构:

但是当我从一个由文件夹组成的网络表单中运行它时,它可以正常运行。

如何使位于公用文件夹内的 aspx 工作?

【问题讨论】:

  • 你需要配置一个公共控制器来路由到你的视图。
  • 那应该怎么样? @NathanChampion
  • @Volazh 你能显示你的 PublicView.aspx 页面的代码吗?
  • @Siavas 我已经在帖子开头发布了公共 aspx 代码!
  • 我的坏@Volazh!

标签: asp.net asp.net-mvc visual-studio model-view-controller bootstrap-4


【解决方案1】:

HTTP 404 页面表明控制器定义中不存在PublicView 控制器操作方法名称。如果您想将 webforms 页面与 MVC 路由路径样式相关联,例如/Public/PublicView 映射到 /Views/Public/PublicView.aspx 而不创建对该页面的控制器操作,MapPageRoute() 方法可能适合您的需要。

只需确保将 webforms 路由路径放在默认 MVC 路由之前,因为 RouteConfig 从上到下评估路由定义,因此首先评估大多数特定路由:

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

    // webforms page route
    routes.MapPageRoute(
        "PublicView",
        "Public/PublicView",
        "~/Views/Public/PublicView.aspx"
    );

    // MVC route
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

如果您想用一个MapPageRoute 映射文件夹内的所有 ASPX 页面,请将其调整为包含页面名称占位符:

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

    // webforms page route
    routes.MapPageRoute(
        "PublicView",
        "Public/{pageName}",
        "~/Views/Public/{pageName}.aspx"
    );

    // MVC route
    routes.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

注意事项:

1) 如果您想使用 ASPX 页面作为从控制器操作方法呈现的 MVC 视图页面,则需要将 Page 指令中的 Inherits 更改为 System.Web.Mvc.ViewPage,如下所示:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" ... %>

然后在从控制器调用页面之前用 MVC HTML 帮助器替换 webforms 服务器控件,如下所示:

public class PublicController : Controller
{
    public ActionResult PublicView()
    {
        return View("PublicView");
    }
}

2) 避免在代码隐藏中直接将 ASPX 页面的 System.Web.UI.Page 命名空间替换为 System.Web.Mvc.ViewPage 命名空间,因为这可能会在使用服务器控件时触发多个问题。

参考:How to define routes for Web Forms applications

【讨论】:

    【解决方案2】:

    这是我的解决方案。它正在工作。希望能帮到你,我的朋友:))

    1) 配置路由信息:

    public static void RegisterRoutes(RouteCollection routes)
            {
                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
                // This informs MVC Routing Engine to send any requests for .aspx page to the WebForms engine
                routes.IgnoreRoute("{resource}.aspx/{*pathInfo}");
                routes.IgnoreRoute("{resource}.aspx");
    
                routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                );
            }
    

    2) 调用控制器中的动作:

    public class PublishController : Controller
        {
            // GET: Publish
            public ActionResult Index()
            {
                return View("WebForm1");
            }        
        }
    

    3) 在 webform 后面的代码中:

    public partial class WebForm1 : System.Web.Mvc.ViewPage
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    //Your code here
                }
            }
        }
    

    【讨论】:

      【解决方案3】:

      您只需要告诉服务器有关您的页面的更多信息,包括其位置。这可以通过&lt;%@ Page &gt; 标记来完成,该标记必须插入到页面的开头,如下所示:

      <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="PublicView.aspx.cs" Inherits="WebApplication1.Public.PublicView" %>
      

      如果您创建的页面是母版页面(这似乎是因为您在其中有您的&lt;head&gt; 标记,则您必须改用Master 并在创建新页面时选择适当的文件类型:

      <%@ Master Language="C#" AutoEventWireup="true" CodeBehind="PublicView.master.cs" Inherits="PublicView.SiteMaster" %>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-01-20
        • 2023-03-24
        • 2020-07-20
        • 2014-06-10
        • 1970-01-01
        • 2016-05-17
        • 2020-12-13
        相关资源
        最近更新 更多