码迷,mamicode.com
首页 > Web开发 > 详细

How to get the Current Controller Name, Action, or ID in ASP.NET MVC

时间:2014-11-27 20:08:24      阅读:489      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   ar   sp   on   div   log   cti   

public static class HtmlRequestHelper
{
    public static string Id(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;

        if (routeValues.ContainsKey("id"))
            return (string)routeValues["id"];
        else if (HttpContext.Current.Request.QueryString.AllKeys.Contains("id"))
            return HttpContext.Current.Request.QueryString["id"];

        return string.Empty;
    }

    public static string Controller(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;

        if (routeValues.ContainsKey("controller"))
            return (string)routeValues["controller"];

        return string.Empty;
    }

    public static string Action(this HtmlHelper htmlHelper)
    {
        var routeValues = HttpContext.Current.Request.RequestContext.RouteData.Values;

        if (routeValues.ContainsKey("action"))
            return (string)routeValues["action"];

        return string.Empty;
    }
}

  

How to get the Current Controller Name, Action, or ID in ASP.NET MVC

标签:blog   http   io   ar   sp   on   div   log   cti   

原文地址:http://www.cnblogs.com/dupeng0811/p/4126888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!