Một số ngày trước, tôi đã viết một bài đăng blog về chủ đề đó (xem How to build absolute action URLs using the UrlHelper class). Như Darin Dimitrov đã đề cập: UrlHelper.Action
sẽ tạo URL tuyệt đối nếu thông số được chỉ định rõ ràng.
Tuy nhiên, tôi đề nghị để viết một phương pháp mở rộng thông thường vì lợi ích của khả năng đọc:
/// <summary>
/// Generates a fully qualified URL to an action method by using
/// the specified action name, controller name and route values.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="actionName">The name of the action method.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The route values.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteAction(this UrlHelper url,
string actionName, string controllerName, object routeValues = null)
{
string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
return url.Action(actionName, controllerName, routeValues, scheme);
}
Phương pháp này sau đó có thể được gọi như thế này: @Url.AbsoluteAction("SomeAction", "SomeController")
Nguồn
2011-07-21 23:10:09
cú pháp đúng là: 'Action (string actionName , string controllerName) ', tham số đầu tiên phải là action và thứ hai là controller –