视图或URL应该ASP.NET MVC多少级深呢? [英] ASP.NET MVC How many levels deep should a view or URL be?

查看:100
本文介绍了视图或URL应该ASP.NET MVC多少级深呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还在学习ASP.NET MVC。随着web表单,我将创建一个新的文件夹让管理员调用它。在那里我可能有create_product,edit_product等众多页所以URL可能看起来像<一个href=\"http://somesite.com/admin/create%5Fproduct.aspx\">http://somesite.com/admin/create%5Fproduct.aspx.

I am still learning ASP.NET MVC. With webforms, I would create a new folder let's call it admin. In there I might have many pages for create_product, edit_product, etc. So the URL might look like http://somesite.com/admin/create%5Fproduct.aspx.

但随着MVC这是一个有点不同。我想看看有什么会做到这一点的最好办法。

But with MVC it is a little different. I am trying to see what would be the best way to do this.

应该做 http://somesite.com/admin/product/create 是正确的?还是应该仅仅是 http://somesite.com/product/create ?如果我做它作为第一个办法,做我把一切都在管理员控制器或应该把它分为产品控制器?

Would doing http://somesite.com/admin/product/create be right? Or should it just be http://somesite.com/product/create? If I do it as the first way, do I put everything in the "admin" controller or should it be separated into a "product" controller?

我知道这可能是主观的还是个人的选择,但我希望得到一些建议。

I know this is probably subjective or personal choice, but I would like to get some advise.

感谢。

推荐答案

ASP.NET MVC的好处之一(和更普遍的URL路由引擎共同在.NET 3.5 SP1的所有的ASP.NET)是网址可以灵活配置映射到你preFER任何文件夹/文件结构。这意味着它比它在WebForms的修改URL的日子里,你已经开始构建项目后,要容易得多。

Part of the benefit of ASP.NET MVC (and more generally, the URL Routing Engine common to all of ASP.NET in .NET 3.5 SP1) is that the URLs can be flexibly configured to map to any folder / file structure you prefer. That means it's much easier than it was in the days of WebForms to modify your URLs after you've started building your project.

要您的具体问题:


  • 一个admin控制器与产品控制器 - 一般来说,指导,以保持控制器集中,使他们更容易测试和维护。出于这个原因,我会建议使用每个对象类型(如产品)单个控制器与你的CRUD操作。你的情况的例子:

  • One Admin Controller vs. Product Controller - In general, the guidance is to keep controllers focused so that they are easier to test and maintain. For that reason, I would suggest using a single controller per object type (like Product) with your CRUD actions. Examples in your case:

/管理/产品/创建

/管理/产品/编辑/ 34或/管理/产品/编辑/红色的鞋(如果的名称是唯一的)

/admin/product/edit/34 or /admin/product/edit/red-shoes (if name is unique)

在两种情况下,创建,编辑,Deatils操作都将在ProductController的。你可能只是定制路线,限制其使用的管理行为(如创建和编辑)(并添加管理员文本到URL),然后详细的行动将是所有游客到您的网站使用。

In either case, the Create, Edit, Deatils actions will all be in the ProductController. You may just have custom routes for the "admin actions" (like Create and Edit) that limit their usage (and add the "admin" text to the URL), and then the Details action would be usable by all visitors to your site.


  • [授权] - 只检查该用户已登录

  • [授权(角色=管理员)] - 限制到特定的用户角色

  • [授权(用户=乔)] - 限制到特定的用户

您甚至可以通过强制执行的权限检查在URL路由,这样创建在您的网站,限制进入这些观点管理视图的定制路线:

You can even create a custom route for "Admin" views in your site and limit access to those views by enforcing your authorization check in the URL routing, like this:

routes.MapRoute(
  "Admin",
  "Admin/{controller}/{action}",
  new { controller = "Product", action = "Index" },
  new { authenticated= new AuthenticatedConstraint()}
);

在哪里AuthenticatedConstraint看起来是这样的:

Where AuthenticatedConstraint looks something like:

using System.Web;
using System.Web.Routing;
public class AuthenticatedConstraint : IRouteConstraint
{
  public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
  {
    return httpContext.Request.IsAuthenticated;
  }
}

斯蒂芬·瓦尔特的博客

好详细信息:
<一href=\"http://stephenwalther.com/blog/archive/2008/08/07/asp-net-mvc-tip-30-create-custom-route-constraints.aspx\">ASP.NET MVC提示#30 -

Good details on Stephen Walther's blog: ASP.NET MVC Tip #30 – Create Custom Route Constraints

这篇关于视图或URL应该ASP.NET MVC多少级深呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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