0

我到处都在使用区域,我想要类似以下的东西:

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

通常我通过执行以下操作来访问事物:

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

但如果我想删除,那么我不得不说

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

使用路线,您如何做到这一点?(顺便说一下,这些路线并不现实,在我的系统中它们比这更简洁)

编辑:这与区域的使用特别相关,这是一项 ASP.NET MVC 2 Preview 2 功能。

4

1 回答 1

0

It would depend on how your routes & controllers are currently structured.

Here's an example route you might want to use.

If you want to be able to call the following route to delete:

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

And let's assume you have a controller called "MyAreaController", with an action of "Delete", and for the sake of simplicity let's assume section and subsection are just strings e.g.:

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

Then you could create a route in the following way (in your Global.asax.cs, or wherever you define your routes):

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
            );

Note: I'd normally define enums for all the possible parameter values. Then the params can be of the appropriate enum type, and you can still use strings in your path. E.g. You could have a "Section" enum that has a "MySection" value.

于 2009-10-23T10:38:32.050 回答