参考在AuthorizeAttribute通用网址参数 [英] Reference generic url parameter in AuthorizeAttribute

查看:160
本文介绍了参考在AuthorizeAttribute通用网址参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的动作有类似的路径 /控制器/动作/ {ID} 我能获得 ID 在通过做AuthorizeAttribute httpContext.Request.RequestContext.RouteData.Values​​ [身份证]

相反,如果它的东西像 /控制器/动作?ID = {ID} 我可以通过做得到它 httpContext.Request.QueryString [ ID]

我需要的另一种方式,如果它是从POST表单数据。

有没有办法说让你放什么在参数名为'ID',不管如何指定的路线?


解决方案

  VAR ID = Request.RequestContext.RouteData.Values​​ [ID]? Request.Params [ID]作为字符串;

或者,如果你想特权GET和支持路由数据的POST参数:

  VAR ID = Request.Params [ID]? Request.RequestContext.RouteData.Values​​ [ID]作为字符串;

If my action has a path like /controller/action/{id} I can get id in an AuthorizeAttribute by doing httpContext.Request.RequestContext.RouteData.Values["id"].

Conversely, if it's something like /controller/action?id={id} I can get it by doing httpContext.Request.QueryString["id"].

I'll need yet another way if it's form data from a POST.

Is there a way to say "Get what you would put in the parameter with name 'id', regardless of how the route is specified?"

解决方案

var id = Request.RequestContext.RouteData.Values["id"] ?? Request.Params["id"] as string;

or if you want to privilege GET and POST parameters in favor of route data:

var id = Request.Params["id"] ?? Request.RequestContext.RouteData.Values["id"] as string;

这篇关于参考在AuthorizeAttribute通用网址参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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