2009-10-23 66 views
0

我使用领域无处不在,我想类似如下:领域和途径

http://localhost/MyArea/MySection/MySubSection/Delete/20 

我一般做以下的事情访问:

http://localhost/MyArea/MySection/MySubSection/20 

但是,如果我想删除然后我不得不说

http://localhost/MyArea/MySection/DeleteEntryFromMySubSection/20 

随着路线,你如何做到这一点? (路线不太现实,在我的系统中它们比这更简洁)

编辑:这与使用Areas,ASP.NET MVC 2 Preview 2功能特别相关。

回答

0

这将取决于您的路线&目前的结构。

下面是您可能想要使用的示例路线。

如果你希望能够调用下面的路线删除:

http://localhost/MyArea/MySection/MySubSection/Delete/20 

而且让我们假设你有一个名为“MyAreaController”控制器,以“删除”的行动,并为求简单假设节和第仅仅是字符串如:

public class MyAreaController : Controller 
{ 
    public ActionResult Delete(string section, string subsection, long id) 
    { 

然后,你可以创建以下列方式的路径(在你的Global.asax.cs,或任何你定义你的路由):

var defaultParameters = new {controller = "Home", action = "Index", id = ""};    

routes.MapRoute("DeleteEntryFromMySubSection", // Route name - but you may want to change this if it's used for edit etc. 
      "{controller}/{section}/{subsection}/{action}/{id}", // URL with parameters 
      defaultParameters // Parameter defaults 
      ); 

注意:我通常会为所有可能的参数值定义枚举。然后,参数可以是适当的枚举类型,并且您仍然可以在路径中使用字符串。例如。你可以有一个具有“MySection”值的“Section”枚举。

+0

我正在使用区域,ASP.NET MVC 2功能。所以我的路线就像'http:// localhost/Area/Controller/Action/Parameter'。有关更多详细信息,请参阅http://haacked.com/archive/2009/10/01/asp.net-mvc-preview-2-released.aspx。 – Kezzer 2009-10-23 10:57:23

+0

噢好吧,谢谢你......“老师”成为“学生”!干杯:-) – 2009-10-23 11:28:50