区分GUID和字符串参数在MVC 3 [英] Differentiate Guid and string Parameters in MVC 3

查看:287
本文介绍了区分GUID和字符串参数在MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用ASP.NET MVC(3或4DP)的出的现成方法定位器,是有办法有一个字符串和GUID之间的MVC框架区分,而不需要解析的控制器动作参数?

Using the out-of-the-box method locators in ASP.NET MVC (3 or 4DP), is there a way to have the MVC framework differentiate between a string and Guid without needing to parse the parameter in the controller action?

使用的例子是对网址

HTTP:// [域] /客户/信息/ F325A917-04F4-4562-B104-AF193C41FA78

http://[domain]/customer/details/F325A917-04F4-4562-B104-AF193C41FA78

执行

public ActionResult Details(Guid guid)

的方法,以及

HTTP:// [域] /客户/信息/比尔 - 盖茨

http://[domain]/customer/details/bill-gates

执行

public ActionResult Details(string id)

方法。

由于没有改变,显然这些方法不明确,具体如下:

With no changes, obviously the methods are ambiguous, as follows:

public ActionResult Details(Guid id)
{
    var model = Context.GetData(id);
    return View(model);
}

public ActionResult Details(string id)
{
    var model = Context.GetData(id);
    return View(model);
}

导致错误

The current request for action 'Details' on controller type 'DataController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Details(System.Guid) on type Example.Web.Controllers.DataController
System.Web.Mvc.ActionResult Details(System.String) on type Example.Web.Controllers.DataController 

我试图使用自定义的约束(依据一href=\"http://stackoverflow.com/questions/2421995/how-can-i-create-a-route-constraint-of-type-system-guid\">How我可以创建类型的路由约束的System.Guid ),试图通过路由推过?

I attempted to use a custom constraint (based on How can I create a route constraint of type System.Guid?) to try and push it through via routing:

routes.MapRoute(
    "Guid",
    "{controller}/{action}/{guid}",
    new { controller = "Home", action = "Index" }, 
    new { guid = new GuidConstraint() }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

和切换的操作签名:

public ActionResult Details(Guid guid)
{
    var model = Context.GetData(guid);
    return View(model);
}

public ActionResult Details(string id)
{
    var model = Context.GetData(id);
    return View(model);
}

约束执行并传递,因此参数被发送到一个动作,但貌似还作为一个字符串,从而暧昧两个方法签名。我希望有什么东西在操作方法如何定位导致的模糊性,因而可以通过自定义模块在插上,找到方法来覆盖。

The constraint executes and passes, thus the argument is sent to an action, but seemingly still as a string, and thus ambiguous to the two method signatures. I expect that there's something in how the action methods are located that causes the ambiguity, and thus could be overridden by plugging in a custom module to locate methods.

同样的结果可以通过解析出字符串参数可以实现,但将是非常好的为简便起见,以避免在行动(更不用说一天以后希望重新使用)。

The same result could be achieved by parsing out the string parameter, but would be really nice for brevity to avoid that logic in the action (not to mention to hopefully reuse someday later).

推荐答案

首先,你必须给他们两个不同的名字disambigute你的方法:

First, you must disambigute your methods by giving them two different names:

public ActionResult DetailsGuid(Guid guid)
{
    var model = Context.GetData(guid);
    return View(model); 
}

public ActionResult DetailsString(string id)
{
    var model = Context.GetData(id);
    return View(model);
} 

接下来,你需要一个自定义路由处理程序来检查请求,并相应地更改方法名:

Next, you need a custom route handler to inspect the request, and change the method name accordingly:

using System.Web.Mvc; 
using System.Web.Routing; 

public class MyRouteHandler : IRouteHandler 
{ 
    public IHttpHandler GetHttpHandler(RequestContext requestContext) 
    { 
        var routeData = requestContext.RouteData; 
        var stringValue = routeData.Values["id"].ToString();
        Guid guidValue; 
        var action = routeData.Values["action"]; 
        if (Guid.TryParse(stringValue, out guidValue) && (guidValue != Guid.Empty);
            routeData.Values["action"] = action + "Guid"; 

        else
            routeData.Values["action"] = action + "String"; 

        var handler = new MvcHandler(requestContext); 
        return handler; 
    } 
} 

最后,在你的路由的顶部添加详细信息路线如下:

routes.Add("Details", 
    new Route("{controller}/Details/{id}", 
        new RouteValueDictionary( 
            new { controller = "Home", action = "Details" }), 
            new MyRouteHandler()
        )
    ); 
);

当一个请求进入细节,详细信息航线将使用您的自定义路由处理程序来检查 ID 令牌。该路由处理程序添加到基于ID令牌的窗体上的动作名称,这样,请求将被引导至适当的行动。

When a request comes in for details, the Details route will use your custom route handler to inspect the id token. The route handler adds to the action name based on the form of the id token, so that the request will be directed to the appropriate action.

这篇关于区分GUID和字符串参数在MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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