【问题标题】:MVC4 - Why is my URL rewrite preventing me from accessing the ID in a custom AuthoriseAttributeMVC4 - 为什么我的 URL 重写阻止我访问自定义 AuthoriseAttribute 中的 ID
【发布时间】:2013-01-17 06:10:02
【问题描述】:

我目前正在 MVC4 中编写一个授权过滤器,它具有默认的 URL 重写

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

通过上面的路线,我有一个自定义的授权属性,像这样开始

protected override bool AuthorizeCore(HttpContextBase httpContext)
{
    String Value = httpContext.Request["id"];

但是 id 没有通过。

当我把路线从它上面移开时。

无论如何我可以在授权属性中拥有路由和 id 吗?

【问题讨论】:

    标签: .net url-rewriting asp.net-mvc-4 custom-attributes


    【解决方案1】:

    对于任何对我的解决方案感兴趣的人,我设法从 RequestContext 中的 RouteData 中获取了我需要的信息 -

        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            String Value = httpContext.Request["id"];
            if(String.IsNullOrWhiteSpace(Value))
                Value = httpContext.Request.RequestContext.RouteData.Values["id"] as String;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-25
      • 2021-09-20
      • 1970-01-01
      • 2022-04-06
      • 2014-10-13
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多