【问题标题】:Portable Area not finding embedded files MVC4 C#便携式区域找不到嵌入文件 MVC4 C#
【发布时间】:2013-02-04 21:02:05
【问题描述】:

我有一个包含两个项目的解决方案。一个可移植区域项目,以及一个引用可移植区域项目的网站,并且两者都引用 MvcContrib。

我遇到的问题是嵌入式资源,当我尝试访问它们时,它们给了我一个 404 错误。似乎它正在尝试访问物理路径而不是 dll。局部视图工作正常。

我尝试访问的文件在我的 Visual Studio 解决方案资源管理器中如下所示

AdHocReporting/Areas/AdHocReportBuilder/Content/adhoc.css(嵌入构建操作)

以下是便携式区域的路线:

using System.Web.Mvc;
using MvcContrib.PortableAreas;

namespace AdHocReporting.Areas.AdHocReportBuilder
{
    public class AdHocReportBuilderAreaRegistration : PortableAreaRegistration
    {
        public override string AreaName
        {
            get { return "AdHocReportBuilder"; }
        }

        public override void RegisterArea(AreaRegistrationContext context, IApplicationBus bus)
        {
            RegisterRoutes(context);
            RegisterAreaEmbeddedResources();

        }
        private void RegisterRoutes(AreaRegistrationContext context)
        {
            context.MapRoute(
              AreaName + "_content",
              base.AreaRoutePrefix + "/Content/{resourceName}",
              new { controller = "EmbeddedResource", action = "Index", resourcePath = "Content" },
              new[] { "MvcContrib.PortableAreas" }
          );

            context.MapRoute(
                AreaName + "_default",
                base.AreaRoutePrefix + "/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

这里是指向便携式区域的网站 Global.aspx

namespace AdHocReportingSite
{
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            PortableAreaRegistration.RegisterEmbeddedViewEngine();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }
    }
}

这是我探索的解决方案的图像:

【问题讨论】:

  • 愚蠢的问题,但是您的嵌入式资源是否将其构建操作设置为“嵌入式资源”?
  • 是的,他们有。我不确定问题是什么,所以我从头开始重建了整个项目,它最终工作了。谢谢。

标签: asp.net-mvc-4 portable-areas


【解决方案1】:

创建新项目并重做所有内容后,无法重现该问题。我不确定这是否是一个错误,或者我只是错过了设置便携式区域的步骤。

【讨论】:

    【解决方案2】:

    在 Web.Config 中为每个文件类型添加:

    <system.webServer>
      <handlers>
        <add name="js" path="*.js" verb="*" type="System.Web.Handlers.TransferRequestHandler" resourceType="File" preCondition="integratedMode" />
      </handlers>
    </system.webServer>
    

    这将使 IIS 尝试使用定义的路由而不是搜索静态文件。

    【讨论】:

    • 很好,下次我会记住的。我现在已经完成了这个模型的工作。谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-08-27
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多