我遇到像link text
这样的问题我的所有链接都是这样的:htp // site / controller / action / id
我刚刚添加了名为 BackEnd 的区域。
我的控制器:
[ActionLinkArea("")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
现在,当我尝试使用
获取一些控制器URL时@Html.ActionLink<HomeController >(c => c.Index(), "Home")
一切正常,网址是htp:// site / HomeController / Index /
但是当我使用Microsoft.Web.Mvc.dll的扩展方法
时 @Html.BuildUrlFromExpression<HomeController>(c => c.Index())
我得到网址htp:// site / BackEnd / HomeController / Index /
如何使用BuildUrlFromExpression获取没有Area的URL以及为什么ActionLink工作正常但是 BuildUrlFromExpression不是吗?
答案 0 :(得分:3)
这是微软的错误。
http://aspnet.codeplex.com/workitem/7764
该方法在内部使用LinkBuilder.BuildUrlFromExpression()。后者调用routeCollection.GetVirtualPath(context,routeValues)而不是routeCollection.GetVirtualPathForArea(context,routeValues);使用区域时会导致无效结果。
我做了,方法返回正确的网址
答案 1 :(得分:2)
我最好回答!
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")%>