html.buildurlfromexpressionコールにデフォルト領域を指定する方法
-
25-09-2019 - |
質問
私には問題があります テキストをリンクします
私のリンクはすべて次のようになります:HTP //サイト/コントローラー/アクション/ID
電話がかかったエリアを追加しました バックエンド.
私のコントローラー:
[ActionLinkArea("")]
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
}
今、私はいくつかのコンロラー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を取得するにはどうすればよいですか。アクションリンクが正常に機能するのはなぜですか?
解決
Microsoftのバグです。
http://aspnet.codeplex.com/workitem/7764
このメソッドは、内部的にlinkbuilder.buildurlfromexpression()を使用します。後者は、RouteCollection.getVirtualPathForarea(Context、RouteValues)の代わりに、RouteCollection.getVirtualPath(コンテキスト、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