ASP.NET MVC 4区在不同的项目不工作(查看未找到) [英] ASP.NET MVC 4 Areas in separate projects not working (view not found)

查看:226
本文介绍了ASP.NET MVC 4区在不同的项目不工作(查看未找到)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个使用在不同的项目领域简单的证明了概念ASP.NET MVC 4 Web站点。

I have tried to create simple proof-of-concept ASP.NET MVC 4 web site using areas in separate projects.

我试着以下教程:<一href=\"http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects\">http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects (应用程序不能在虚拟目录工作...我使用IIS)。我希望有比虚拟目录更好的办法。

I tried to following tutorials: http://bob.archer.net/content/aspnet-mvc3-areas-separate-projects (Application doesn't work in virtual directory... I use IIS). I hope there is better way than virtual directories.

然后我尝试这个教程: http://forums.asp.net/t/1483660.aspx/1
但是,在区域项目的* .csproj的没有AreasManifestDir元素(和遇到错误的观点索引或找不到它的主人或无视图引擎支持搜索位置)

Then I tried this tutorial: http://forums.asp.net/t/1483660.aspx/1 But there is no "AreasManifestDir" element in *.csproj of area project (and got error "The view 'Index' or its master was not found or no view engine supports the searched locations")

是否还有在ASP.NET支持MVC 4?因为我发现这个答案,它可以在将来被移除:<一href=\"http://stackoverflow.com/questions/1526631/what-are-the-pros-and-cons-of-areas-implemented-as-single-projects-vs-multiple-p\">What一些领域作为单个项目VS在asp.net多个项目MVC

Is there still support in ASP.NET for MVC 4? Because I found this answer that it can be removed in future: What are the pros and cons of Areas implemented as single projects vs multiple projects in asp.net mvc

我还没有发现任何获取为MVC4。

I haven't found any how-to for MVC4.

解决方案结构简单:

Solution 'MvcAreasMultiProject'
    Areas [Directory]
        Admin [Project]
        Models
        Views
        Controllers
        Routes.cs [Examples]
    MvcAreasMultiProject [MainProject]
        - References Admin project
        M.V.C

联系项目Routes.cs:

Routes.cs of Admin project:

namespace Admin
{
public class Routes : AreaRegistration
{
    public override string AreaName { get { return "Admin"; } }

    public override void RegisterArea(AreaRegistrationContext context)
    {
        context.MapRoute(
            "Admin_Default",
            "Admin/{action}/{id}",
            new { controller = "Admin", action = "Index", id = "" },
            new[] { "Admin.Controllers" }
        );
    }
}
}

感谢您的帮助!

推荐答案

您可以使用 RazorGenerator 包嵌入您的剃刀意见纳入一个单独的程序。下面是使这项工作的步骤:

You could use the RazorGenerator package to embed your Razor views into a separate assembly. Here are the steps to make this work:


  1. 安装剃须刀发电机的Visual Studio扩展(工具 - >扩展和更新...)

  2. 创建使用空模板创建新的ASP.NET MVC应用4。

  3. 添加新的类库项目 AreasLibrary 称为解决方案(为了你也可以使用一个ASP.NET MVC项目模板在剃刀的观点得到智能感知)

  4. 安装 RazorGenerator.Mvc 的NuGet到 AreasLibrary 项目。

  5. 添加控制器 AreasLibrary 项目中(〜/地区/行政/控制器/ HomeController.cs

  1. Install the Razor Generator Visual Studio extension (Tools -> Extensions and Updates...)
  2. Create a new ASP.NET MVC 4 application using the empty template.
  3. Add a new class library project to the solution called AreasLibrary (you could also use an ASP.NET MVC project template in order to get Intellisense in Razor views)
  4. Install the RazorGenerator.Mvc NuGet to the AreasLibrary project.
  5. Add a controller inside the AreasLibrary project (~/Areas/Admin/Controllers/HomeController.cs):

public class HomeController: Controller
{
    public ActionResult Index()
    {
        return View();
    }
}


  • 添加相应的视图(〜/地区/行政/浏览/首页/ Index.cshtml

    @* Generator: MvcView *@
    
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    
    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title>View1</title>
    </head>
    <body>
        <div>
            Index view        
        </div>
    </body>
    </html>
    


  • 在视图的属性中设置的自定义工具 RazorGenerator

    在类库中添加〜/地区/ AdminAreaRegistration.cs

    public class AdminAreaRegistration : AreaRegistration
    {
        public override string AreaName { get { return "Admin"; } }
    
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "Admin_Default",
                "Admin/{action}/{id}",
                new { controller = "Home", action = "Index", id = "" }
            );
        }
    }
    


  • 所有剩下的就是引用类库在主MVC应用程序。

  • All that's left is to reference the class library in the main MVC application.

    参考:<一href=\"http://blog.davidebbo.com/2011/06/$p$pcompile-your-mvc-views-using.html\">http://blog.davidebbo.com/2011/06/$p$pcompile-your-mvc-views-using.html

    这篇关于ASP.NET MVC 4区在不同的项目不工作(查看未找到)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆