I want to have multiple path folder structure that contain same name views:
我想有多个包含相同名称视图的路径文件夹结构:
/profile.aspx
/admin/profile.aspx
/user/editpost.aspx
/admin/editpost.aspx
/Controllers
|- PostController.cs
|- ProfileController.cs
I want to be able to have all the regular pages in a folder and the admin pages in another folder. Do I need to organize my Views folder like:
我希望能够将所有常规页面放在一个文件夹中,将管理页面放在另一个文件夹中。我是否需要组织我的Views文件夹,如:
/Views
/User
/Story
|- editpost.aspx
/Profile
|- profile.aspx
/Admin
/Story
|_ editpost.aspx
/Web
|- profile.aspx
or is there a way I can do this:
或者我有办法做到这一点:
/User
/Views
/Story
|- editpost.aspx
/Profile
|- profile.aspx
/Admin
/Views
/Story
|_ editpost.aspx
/Web
|- profile.aspx
Also, how do I code/organize/use separate controllers for /User and /Admin that potentially have the same name?
另外,如何为/ User和/ Admin编写/组织/使用可能具有相同名称的单独控制器?
Let me know if I have been unclear.
如果我不清楚,请告诉我。
Thanks!
6 个解决方案
#1
One of the major problems with the first release (And all the RC and Beta's of course) is that ASP.NET MVC does not support areas. Areas are something that alternative MVC frameworks for ASP.NET have supported for some time and when your project gets to a reasonable size you're going to end up with possibly hundreds of controllers (all with unique names) in the same folder and your code is going to be very hard to sort through.
第一个版本(当然还有所有RC和Beta)的主要问题之一是ASP.NET MVC不支持区域。区域是ASP.NET的替代MVC框架已经支持了一段时间,当您的项目达到合理的大小时,您最终可能会在同一文件夹和代码中使用数百个控制器(所有控制器都具有唯一名称)将很难整理。
Your idea makes perfect sense and I hope that future instances of the ASP.NET MVC framework supports areas out of the box (so to speak). In the mean time it's easy to create your own Areas framework on top of ASP.NET MVC.
您的想法非常有意义,我希望ASP.NET MVC框架的未来实例支持开箱即用的区域(可以这么说)。同时,在ASP.NET MVC之上创建自己的区域框架很容易。
Here are some posts that will help you out:
以下是一些可以帮助您的帖子:
- Phil Haack's Post Posted already by çağdaş
- Steve Sanderson's Response A response by Steve to Phil's post taking it futher
- My Post on Localisation using Areas
- Another one of my posts on areas with strongly typed view names
Phil Haack的帖子已经由çağdaş发布了
史蒂夫桑德森的回应史蒂夫对菲尔的帖子做出的回应更进一步
我的使用区域本地化的帖子
另一篇关于具有强类型视图名称的区域的帖子
Hopefully they're helpful to you.
希望他们对你有所帮助。
#2
No problem. You can organise your folders in any way you choose. You can specify a view by name or even by its path in the Action method:
没问题。您可以以您选择的任何方式组织文件夹。您可以按名称指定视图,甚至可以通过Action方法中的路径指定视图:
return View("~/Views/Posts/Index.aspx");
#3
You should read this post by Phil Haack.
你应该阅读Phil Haack的这篇文章。
Basically, you're gonna have to create your own ViewEngine to work with your folder design.
基本上,您将需要创建自己的ViewEngine来处理文件夹设计。
#4
There's another option too:
you might want to create custom ViewEngine and specify view/partialview locations.
还有另一种选择:您可能想要创建自定义ViewEngine并指定视图/部分视图位置。
For example:
//Global.asax
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
...
ViewEngines.Engines.Add(new ViewEngine());
...
}
}
public class ViewEngine : WebFormViewEngine
{
public ViewEngine()
{
PartialViewLocationFormats = PartialViewLocationFormats
.Union(new[] {"~/Views/{1}/Partial/{0}.ascx"});
}
}
allows you to use 'Partial' folder for partial views.
允许您对部分视图使用“部分”文件夹。
But i would personally prefer Areas. Seems that's exactly what you need.
但我个人更喜欢区域。似乎这正是您所需要的。
#5
More than likely each edit view is going to require different fields so sharing editpost is not really viable.
每个编辑视图很可能需要不同的字段,因此共享editpost并不可行。
If however you are editing [exactly] the same fields then perhaps organise the views as you have them and then render a partialview which you can pass a model to.
但是,如果您正在编辑[完全]相同的字段,那么可能会按照您的方式组织视图,然后渲染可以将模型传递给的部分视图。
The partialView can be in a common place which keeps your seperation of concerns as far as the views are concerned as well as code reuse with the partialview.
partialView可以在一个共同的位置,它可以保留您关注视图的关注点以及部分视图的代码重用。
Each controller then has its own name such as UserController and AdminController. Inside each of these you have your editpost action no probs.
然后每个控制器都有自己的名称,如UserController和AdminController。在其中每个都有你的editpost动作没有probs。
Does this help or do you need more?
这有帮助还是需要更多?
#6
Did you try passing the specific view in the Controllers? I think you could bypass MVC common Views folder design by passing the specific view on any controller (However I will not recommend it).
您是否尝试在控制器中传递特定视图?我认为您可以通过在任何控制器上传递特定视图来绕过MVC公共视图文件夹设计(但我不会推荐它)。
I am not 100% sure but I think you could do things like
我不是100%肯定,但我认为你可以做的事情
return View("User/Story"); //or something in that matter
But then you will need to change your routes, so the controllers get routed accordingly. I think I read it some time ago, but I could not found the reference. Let me know if it works.
但是,您需要更改路线,以便控制器相应地进行路由。我想我前一段时间读过它,但我找不到参考文献。如果有效,请告诉我。
However, if your Views are dictating your design, then maybe you should not use MVC after all.
但是,如果您的视图正在指示您的设计,那么也许您不应该使用MVC。
#1
One of the major problems with the first release (And all the RC and Beta's of course) is that ASP.NET MVC does not support areas. Areas are something that alternative MVC frameworks for ASP.NET have supported for some time and when your project gets to a reasonable size you're going to end up with possibly hundreds of controllers (all with unique names) in the same folder and your code is going to be very hard to sort through.
第一个版本(当然还有所有RC和Beta)的主要问题之一是ASP.NET MVC不支持区域。区域是ASP.NET的替代MVC框架已经支持了一段时间,当您的项目达到合理的大小时,您最终可能会在同一文件夹和代码中使用数百个控制器(所有控制器都具有唯一名称)将很难整理。
Your idea makes perfect sense and I hope that future instances of the ASP.NET MVC framework supports areas out of the box (so to speak). In the mean time it's easy to create your own Areas framework on top of ASP.NET MVC.
您的想法非常有意义,我希望ASP.NET MVC框架的未来实例支持开箱即用的区域(可以这么说)。同时,在ASP.NET MVC之上创建自己的区域框架很容易。
Here are some posts that will help you out:
以下是一些可以帮助您的帖子:
- Phil Haack's Post Posted already by çağdaş
- Steve Sanderson's Response A response by Steve to Phil's post taking it futher
- My Post on Localisation using Areas
- Another one of my posts on areas with strongly typed view names
Phil Haack的帖子已经由çağdaş发布了
史蒂夫桑德森的回应史蒂夫对菲尔的帖子做出的回应更进一步
我的使用区域本地化的帖子
另一篇关于具有强类型视图名称的区域的帖子
Hopefully they're helpful to you.
希望他们对你有所帮助。
#2
No problem. You can organise your folders in any way you choose. You can specify a view by name or even by its path in the Action method:
没问题。您可以以您选择的任何方式组织文件夹。您可以按名称指定视图,甚至可以通过Action方法中的路径指定视图:
return View("~/Views/Posts/Index.aspx");
#3
You should read this post by Phil Haack.
你应该阅读Phil Haack的这篇文章。
Basically, you're gonna have to create your own ViewEngine to work with your folder design.
基本上,您将需要创建自己的ViewEngine来处理文件夹设计。
#4
There's another option too:
you might want to create custom ViewEngine and specify view/partialview locations.
还有另一种选择:您可能想要创建自定义ViewEngine并指定视图/部分视图位置。
For example:
//Global.asax
public class MvcApplication : HttpApplication
{
protected void Application_Start()
{
...
ViewEngines.Engines.Add(new ViewEngine());
...
}
}
public class ViewEngine : WebFormViewEngine
{
public ViewEngine()
{
PartialViewLocationFormats = PartialViewLocationFormats
.Union(new[] {"~/Views/{1}/Partial/{0}.ascx"});
}
}
allows you to use 'Partial' folder for partial views.
允许您对部分视图使用“部分”文件夹。
But i would personally prefer Areas. Seems that's exactly what you need.
但我个人更喜欢区域。似乎这正是您所需要的。
#5
More than likely each edit view is going to require different fields so sharing editpost is not really viable.
每个编辑视图很可能需要不同的字段,因此共享editpost并不可行。
If however you are editing [exactly] the same fields then perhaps organise the views as you have them and then render a partialview which you can pass a model to.
但是,如果您正在编辑[完全]相同的字段,那么可能会按照您的方式组织视图,然后渲染可以将模型传递给的部分视图。
The partialView can be in a common place which keeps your seperation of concerns as far as the views are concerned as well as code reuse with the partialview.
partialView可以在一个共同的位置,它可以保留您关注视图的关注点以及部分视图的代码重用。
Each controller then has its own name such as UserController and AdminController. Inside each of these you have your editpost action no probs.
然后每个控制器都有自己的名称,如UserController和AdminController。在其中每个都有你的editpost动作没有probs。
Does this help or do you need more?
这有帮助还是需要更多?
#6
Did you try passing the specific view in the Controllers? I think you could bypass MVC common Views folder design by passing the specific view on any controller (However I will not recommend it).
您是否尝试在控制器中传递特定视图?我认为您可以通过在任何控制器上传递特定视图来绕过MVC公共视图文件夹设计(但我不会推荐它)。
I am not 100% sure but I think you could do things like
我不是100%肯定,但我认为你可以做的事情
return View("User/Story"); //or something in that matter
But then you will need to change your routes, so the controllers get routed accordingly. I think I read it some time ago, but I could not found the reference. Let me know if it works.
但是,您需要更改路线,以便控制器相应地进行路由。我想我前一段时间读过它,但我找不到参考文献。如果有效,请告诉我。
However, if your Views are dictating your design, then maybe you should not use MVC after all.
但是,如果您的视图正在指示您的设计,那么也许您不应该使用MVC。