C#MVC获取当前视图/动态模板

时间:2015-06-25 14:35:29

标签: c# asp.net-mvc-4 razor view

我正在尝试返回当前的动态视图,以允许我将一个css类附加到ActionLink,如果当前视图与ActionLink相同。

当我通过特定路径传递大部分链接时,在本例中为Pages,currentAction在大多数情况下始终为Pages,尽管从ActionResult返回的实际视图或模板被调用。

例如,如果网址是http://mytestdomain.com/sport,我希望currentAction是Sport而不是Pages。

请参阅下面的代码:

RouteConfig.cs

routes.MapRoute("Pages", "{mainCategory}/{subCategory}/{pageName}", new { controller = "Home", action = "Pages", subCategory = UrlParameter.Optional, pageName = UrlParameter.Optional });

的HomeController

public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
    var currentController = htmlHelper.ViewContext.ParentActionViewContext.RouteData.GetRequiredString("controller");
    var currentAction = htmlHelper.ViewContext.ParentActionViewContext.RouteData.GetRequiredString("action");
    var currentView = htmlHelper.CurrentViewName();

    var builder = new TagBuilder("li")
    {
        InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
    };

    builder.AddCssClass("dropdown");

    var actionSplit = actionName.TrimStart('/').Split('/');

    actionName = actionSplit[0];

    if (controllerName == currentController && actionName == currentAction)
    {
        return new MvcHtmlString(builder.ToString().Replace("a href", "a class=\"active\" href").Replace("</li>", "").Replace("Home/", ""));
    }

    return new MvcHtmlString(builder.ToString().Replace("</li>", "").Replace("Home/", ""));
}

public static string CurrentViewName(this HtmlHelper html)
{
        return System.IO.Path.GetFileNameWithoutExtension(((RazorView)html.ViewContext.View).ViewPath);
    }

public ActionResult Pages(string mainCategory, string subCategory, string pageName)
{
    if (!string.IsNullOrEmpty(pageName))
    {
        subCategory = subCategory + "/" + pageName;
    }

    Page model;

    using (CMSEntities)
    {
        model = (from f in CMSEntities.GetPage(1, mainCategory, subCategory, "Live") select f).FirstOrDefault();
    }        

    return View(model.Template, model);
}

Navigation.cshtml

@Html.MenuLink(navigation.Title, "/" + Html.ToFriendlyUrl(navigation.Title), "Home")

我尝试过使用var currentView = htmlHelper.CurrentViewName();但是这将始终返回导航,因为从[ChildActionOnly] public ActionResult Navigation()中调用ActionLink,例如{/ 1}}来自Views / Shared / _Layout.cshtml

非常感谢任何帮助: - )

1 个答案:

答案 0 :(得分:0)

最后我使用了&#39; HttpContext.Current.Request.Url.AbsolutePath&#39;确定将活动类附加到匹配页面链接的当前位置。

public static MvcHtmlString MenuLink(this HtmlHelper htmlHelper, string linkText, string actionName, string controllerName)
{
    var currentController = htmlHelper.ViewContext.ParentActionViewContext.RouteData.GetRequiredString("controller");
    var currentUrl = HttpContext.Current.Request.Url.AbsolutePath.TrimStart('/').Split('/');

    var mainCategory = currentUrl[0];

    var builder = new TagBuilder("li")
    {
        InnerHtml = htmlHelper.ActionLink(linkText, actionName, controllerName).ToHtmlString()
    };

    builder.AddCssClass("dropdown");

    var actionSplit = actionName.TrimStart('/').Split('/');

    actionName = actionSplit[0];

    if (actionSplit.Length == 1)
    {
        if (controllerName == currentController && actionName == mainCategory)
        {
            return new MvcHtmlString(builder.ToString().Replace("a href", "a class=\"active\" href").Replace("</li>", "").Replace("Home/", ""));
        }
    }

    return new MvcHtmlString(builder.ToString().Replace("</li>", "").Replace("Home/", ""));
}

我希望这对其他人有用: - )