如何将默认区域指定至html.buildurlfromexpression调用
-
25-09-2019 - |
题
我有问题 链接文字
我所有的链接看起来都这样:htp // site/controller/action/id
我刚刚添加了名为 后端.
我的控制器:
[ActionLinkArea("")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
现在,当我尝试使用一些conroller URL时
@Html.ActionLink<HomeController >(c => c.Index(), "Home")
所有功能正常,URL为htp:// site/homecontroller/index/
但是,当我从Microsoft.web.mvc.dll使用扩展方法时
@Html.BuildUrlFromExpression<HomeController>(c => c.Index())
我得到URL htp:// site/后端/homecontroller/index/
我如何使用BuildUrlfromeXpression在没有区域的情况下获取URL,为什么ActionLink可以正常工作但buildurlfromexpression却没有?
解决方案
这是Microsoft错误。
http://aspnet.codeplex.com/workitem/7764
该方法使用内部linkbuilder.buildurlfromexpression()。后者调用RouteCollection.getVirtualPath(上下文,Routevalues),而不是RouteCollection.getVirtualPathForarea(context,Routevalues);使用区域时会导致无效的结果。
我做到了,方法返回正确的URL
其他提示
我有更好的答案!
public static string Image<T>(this HtmlHelper helper, Expression<Action<T>> action, int width, int height, string alt)
where T : Controller
{
var expression = action.Body as MethodCallExpression;
string actionMethodName = string.Empty;
if (expression != null)
{
actionMethodName = expression.Method.Name;
}
string url = new UrlHelper(helper.ViewContext.RequestContext, helper.RouteCollection).Action(actionMethodName, typeof(T).Name.Remove(typeof(T).Name.IndexOf("Controller"))).ToString();
//string url = LinkBuilder.BuildUrlFromExpression<T>(helper.ViewContext.RequestContext, helper.RouteCollection, action);
return string.Format("<img src=\"{0}\" width=\"{1}\" height=\"{2}\" alt=\"{3}\" />", url, width, height, alt);
}
<%=Html.Image<ClassController>(c => c.Index(), 120, 30, "Current time")%>
不隶属于 StackOverflow