本文介绍了获取当前控制器和动作发送到路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取当前控制器和动作被称为的路线。在我的Global.asax.cs我下面的使用,并具有以下行:

I am attempting to Get the current controller and action being called in the route. In my Global.asax.cs I have the following using and the following line:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using IProwlAdminUI.Models;

*** LINE WITH ISSUE***
string action = ViewContext.RouteData.Values["controller"].ToString() + ViewContext.RouteData.Values["action"].ToString()
*******

眼下ViewContext.RouteData是给我需要的对象引用非静态字段,方法或属性System.Web.Mvc.ControllerContext.RouteData.Get'。

Right now the ViewContext.RouteData is giving me "an object reference is required for the non-static field, method, or property 'System.Web.Mvc.ControllerContext.RouteData.Get'"

我需要得到这个,这样我可以使用控制器和行动,以评估该用户权限,看看他们是否能执行他们试图做的动作。

I need to get this so that I can use the controller and action to evaluate permissions for the user and see if they can perform the action they are attempting to do.

在这个任何帮助将大大AP preciated。

Any Help on this would be greatly appreciated.

感谢

推荐答案

您不能做你正在尝试做的,因为在Global.asax中的code当你的应用程序启动时执行一次。此事件发生之前传入请求的情况下可用,这就是为什么你不能访问的RouteData值。

You cannot do what you are trying to do because the code in Global.asax executes only once when your application starts up. This happens before the context of an incoming request is available and that is why you cannot access the RouteData values.

在一个更一般的笔记,你可以写一个自定义AuthorizationAttribute做你想要什么。请参阅以下链接的例子:http://davidhayden.com/blog/dave/archive/2009/04/09/CustomAuthorizationASPNETMVCFrameworkAuthorizeAttribute.aspx

On a more general note, you can write a custom AuthorizationAttribute to do what you want. See the following link for an example: http://davidhayden.com/blog/dave/archive/2009/04/09/CustomAuthorizationASPNETMVCFrameworkAuthorizeAttribute.aspx

这篇关于获取当前控制器和动作发送到路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 03:18
查看更多