mvc获得当前页面html代码,C# MVC获取当前视图/动态模板

I am trying to return the current dynamic View to allow me to append a css class to an ActionLink if the current View is the same as the ActionLink.

As I am passing the majority of links through a specific route, in this case Pages, the currentAction will always be Pages in most cases, despite the actual View or Template being returned from the ActionResult called.

So for example if the url is http://mytestdomain.com/sport I would like the currentAction to be Sport and not Pages.

Please see my code below:

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("", "").Replace("Home/", ""));

}

return new MvcHtmlString(builder.ToString().Replace("", "").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")

I have tried using var currentView = htmlHelper.CurrentViewName(); but this will always return Navigation as the ActionLink is being called from within a [ChildActionOnly] public ActionResult Navigation() for example @{ Html.RenderAction("Navigation", "Home"); } from within Views/Shared/_Layout.cshtml

Any help would be much appreciated :-)

Solutions1

In the end I used 'HttpContext.Current.Request.Url.AbsolutePath' to determine the current location to append the active class to the matching page link.

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("", "").Replace("Home/", ""));

}

}

return new MvcHtmlString(builder.ToString().Replace("", "").Replace("Home/", ""));

}

I hope this proves useful to others :-)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值