应视图文件/目录结构在ASP.NET MVC是什么? [英] What should the view file/directory structure be in ASP.NET MVC?

查看:79
本文介绍了应视图文件/目录结构在ASP.NET MVC是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很困惑与意见如何组织,并了解这是ASP.NET MVC使用约定得到的一切工作是很重要的权利。

I'm confused with how views are organized, and it is important to understand this as ASP.NET MVC uses conventions to get everything working right.

根据该意见目录中,有子目录。这些子目录里面是美景。我假定子目录映射到控制器和控制器作用于包含其子目录中的意见。

Under the views directory, there are subdirectories. Inside these subdirectories are views. I'm assuming that the subdirectories map to controllers, and the controllers act on the views contained within their subdirectories.

有没有都包含在这些目录中什么类型的意见新兴的期望呢?例如,应该为每一个目录的默认页面的Index.aspx?如果页面遵循命名约定如创建[控制器]的.aspx,列表[控制器]的.aspx等?或者它不要紧?

Is there an emerging expectation of what types of views are contained within these directories? For instance, should the default page for each directory be index.aspx? Should the pages follow a naming convention such as Create[controller].aspx, List[controller].aspx, etc? Or does it not matter?

推荐答案

查看目录命名和文件命名是很重要的,因为ASP.NET MVC框架使得某些关于他们的假设。如果不符合这些假设,那么你必须写code,让框架知道你在做什么。一般来说,你应该,除非你有一个很好的理由不符合这些假设。

View directory naming and file naming are important, because the ASP.NET MVC framework makes certain assumptions about them. If you do not conform to these assumptions, then you must write code to let the framework know what you are doing. Generally speaking, you should conform to these assumptions unless you have a good reason not to.

让我们来看看最简单的可能控制器操作:

Let's look at the simplest possible controller action:

    public ActionResult NotAuthorized()
    {
        return View();
    }

由于没有视图名称在调用指定查看(),该框架将presume该视图文件名是相同的动作名称。该框架有一个称为视图引擎类型,这将提供扩展。默认的视图引擎是WebFormViewEngine,这将需要该名称和一个.aspx追加到它。因此,在这种情况下,完整的文件名会NotAuthorized.aspx。

Because no view name has been specified in the call to View(), the framework will presume that the view filename will be the same as the Action name. The framework has a type called ViewEngine which will supply the extension. The default ViewEngine is WebFormViewEngine, which will take that name and append an .aspx to it. So the full filename in this case would be NotAuthorized.aspx.

不过,在哪个文件夹将在文件中发现了什么?同样,视图引擎提供的信息。随着WebFormViewEngine,它看起来在两个文件夹:〜/查看/共享和〜/查看/ {}控制器

But in which folder will the file be found? Again, the ViewEngine supplies that information. With WebFormViewEngine, it will look in two folders: ~/Views/Shared and ~/Views/{controller}

所以,如果你的控制器被称为的AccountController,它看起来在〜/查看/帐户

So if your controller was called AccountController, it would look in ~/Views/Account

但有可能是时候,你不想遵循这些规则。例如,两种不同的操作可能会返回同一视图(使用不同的模型,或东西)。在这种情况下,如果你在你的行动明确指定视图名称:

But there might be times when you don't want to follow these rules. For instance, two different actions might return the same view (with a different model, or something). In this case, if you specify the view name explicitly in your action:

    public ActionResult NotAuthorized()
    {
        return View("Foo");
    }

请注意,与WebFormViewEngine,查看名一般是相同的文件名,扩展名少,但框架不需要其他视图引擎的

Note that with WebFormViewEngine, the "view name" is generally the same as the filename, less the extension, but the framework does not require that of other view engines.

同样,你也可能有一个理由来想你的应用程序,寻找意见和非默认的文件夹。您可以通过创建自己的ViewEngine做到这一点。我显示了ASP.NET MVC\">在,但类型名称是不同的,因为它是为框架的早期版本编写的。其基本思路是仍然是相同的,但是

Similarly, you might also have a reason to want your application to look for views and non-default folders. You can do that by creating your own ViewEngine. I show the technique in this blog post, but the type names are different, since it was written for an earlier version of the framework. The basic idea is still the same, however.

这篇关于应视图文件/目录结构在ASP.NET MVC是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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