类ASP.NET MVC C#获取控制器和动作名称 [英] ASP.NET MVC C# Get controller and action name in class

查看:1630
本文介绍了类ASP.NET MVC C#获取控制器和动作名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是非常新的StackOverflow所以请原谅我的无知的迹象。 :)



我有在Visual Studio 2010(C#编写的控制器)MVC应用程序有点小问题。
我想创建它生成一个应用程序操作历史记录的方法,并为此目的我想获取在每一次使用的控制器和动作的名称。不幸的是持有我的控制器名称字符串的第一个字母总是缺少。
我用这个代码:

 字符串URL = HttpContext.Current.Request.RawUrl; 
的RouteData路线= RouteTable.Routes.GetRouteData(新OwnedContext(URL));
VAR值= route.Values;
串controllerName =值[控制器]的ToString();
串actionName =值[行动]的ToString();



在哪里OwnedContext是这样定义的:

 私有类OwnedContext:HttpContextBase 
{
私人只读HttpRequestBase mockHttpRequestBase;

公共OwnedContext(字符串appRelativeUrl)
{
this.mockHttpRequestBase =新OwnedRequest(appRelativeUrl);
}

公众覆盖HttpRequestBase请求
{
{返回mockHttpRequestBase; }
}
}



动作名称正确保存,但是当我调试这个代码我看到controllerName字符串持有控制器,但第一个(资本)的来信总是缺少的名字,即使URL字符串持有这种模式的值:/控制器/动作。



我会明白任何指针,代码示例或者解释为什么会这样。如果我的描述是不准确的让我知道,我会改进



在此先感谢:)



编辑:溶液中发现的:



发现问题(排序):有什么问题OwnedContext(在我原来的问题定义)。起初,我用routeValueDictionary作为HarHaHu建议,但原来的问题依然存在,直到我把HttpContext的是GetRouteData的参数:

 字符串URL =的HttpContext .Current.Request.RawUrl; 
的RouteData路线= RouteTable.Routes.GetRouteData(HttpContext的);
UrlHelper urlHelper =新UrlHelper(新的RequestContext(HttpContext的,路线));

VAR routeValueDictionary = urlHelper.RequestContext.RouteData.Values;
串controllerName = routeValueDictionary [控制器]的ToString();
串actionName = routeValueDictionary [行动]的ToString();



在哪里的HttpContext有一个自定义的getter:

 公开新HttpContextBase的HttpContext 
{
得到
{
HttpContextWrapper背景=
新HttpContextWrapper(System.Web.HttpContext.Current );
回报(HttpContextBase)范围内;
}
}



这样,我省略了OwnedContext终于拿到了我的控制器全名(例如:家具,而不是urniture)。



感谢您的提示。 :)
希望这可以帮助别人。祝你好运!


解决方案

  VAR routeValueDictionary = urlHelper.RequestContext.RouteData.Values; 



与您的自定义背景下使用此功能。


I'm pretty much new to StackOverflow so please forgive any signs of my ignorance. :)

I have a somewhat minor issue with an MVC application in Visual Studio 2010 (Controllers written in C#). I want to create a method which generates an application action history and for that purpose I want to get the name of the controller and action that are used each time. Unfortunately the first letter of the string which holds my controller name is always missing. I used this code:

    string url = HttpContext.Current.Request.RawUrl;
    RouteData route = RouteTable.Routes.GetRouteData(new OwnedContext(url));
    var values = route.Values;
    string controllerName = values["controller"].ToString();
    string actionName = values["action"].ToString();

Where OwnedContext is defined like this:

    private class OwnedContext : HttpContextBase
    {
        private readonly HttpRequestBase mockHttpRequestBase;

        public OwnedContext(string appRelativeUrl)
        {
            this.mockHttpRequestBase = new OwnedRequest(appRelativeUrl);
        }

        public override HttpRequestBase Request
        {
            get { return mockHttpRequestBase; }
        }
    }

The action name is stored correctly but when i debug this code i see that the controllerName string holds the name of the controller but the first (Capital) letter is always missing, even though the url string holds a value with this pattern: /controller/action.

I will appreciate any pointers, code examples or an explanation why this happens. If my description is not accurate let me know, I will improve it.

Thanks in advance :)

EDIT: SOLUTION FOUND:

Found the problem (sort of): There was something wrong with OwnedContext (defined in my original question). At first I used routeValueDictionary as HarHaHu suggested but the original problem persisted, until I placed httpContext as GetRouteData's parameter:

    string url = HttpContext.Current.Request.RawUrl;
    RouteData route = RouteTable.Routes.GetRouteData(httpContext);
    UrlHelper urlHelper = new UrlHelper(new RequestContext(httpContext, route));

    var routeValueDictionary = urlHelper.RequestContext.RouteData.Values;
    string controllerName = routeValueDictionary["controller"].ToString();
    string actionName = routeValueDictionary["action"].ToString();

Where httpContext has a custom getter:

    public new HttpContextBase httpContext
    {
        get
        {
            HttpContextWrapper context =
                new HttpContextWrapper(System.Web.HttpContext.Current);
            return (HttpContextBase)context;
        }
    }

This way I omitted the OwnedContext and finally got my controller's full name (for example: Furniture and not urniture).

Thanks for the tips. :) Hope this helps someone. Good luck!

解决方案

var routeValueDictionary = urlHelper.RequestContext.RouteData.Values;

use this with your custom context.

这篇关于类ASP.NET MVC C#获取控制器和动作名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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