route.config中的默认URL将作为区域中操作方法的url放置

时间:2016-06-07 07:33:09

标签: c# asp.net-mvc asp.net-mvc-4 routes

我希望在中调用默认操作方法应该是route.config的操作方法。所以我做了类似的事情:

area

此处 - public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" }); routes.MapRoute( name: "Default", url: "{culture}/{controller}/{action}/{id}", defaults: new { culture = "en", controller = "LocationCon", action = "LocationIndex", id = UrlParameter.Optional }, namespaces: new[] { "Locator.Areas.LocationArea.Controllers" } ); } 是操作名称,LocationIndex是控制器,LocationCon是区域。 这不起作用。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为No resource found错误源自MVC视图引擎,其中您的视图引擎仍然使用默认参数来查找正确的cshtml文件,甚至动作方法也成功执行。

AFAIK,默认情况下MVC视图引擎搜索Project/Views/ControllerNameProject/Views/Shared目录中的Project/Culture/ControllerName未包含的视图文件。

您可以配置自定义视图引擎以搜索相应的视图文件,如下例所示:

using System.Web.Mvc;

public class ViewEngine : RazorViewEngine
{
    public ViewEngine()
    {
            MasterLocationFormats = new[]
            {
                 // your layout cshtml files here
            };

            ViewLocationFormats = new[]
            {
                 // your view cshtml files here. {0} = action name, {1} = controller name
                 // example:
                 "~/Culture/{1}/{0}.cshtml"
            };
            PartialViewLocationFormats = ViewLocationFormats;

            FileExtensions = new[]
            {
                "cshtml"
            };
    }
}

因此,在Global.asax文件上将您的自定义视图引擎置于Application_Start方法:

protected void Application_Start()
{
      AreaRegistration.RegisterAllAreas();
      ...
      // other code here
      ...
      ViewEngines.Engines.Add(new ViewEngine()); // add your custom view engine above
}

CMIIW。