Pet*_*nce 10 asp.net-mvc url-redirection
我想使用RedirectToRouteResult重定向到像/ users/4#Summary这样的URL.使用ASP.NET MVC 1.0,我无法找到一种方法 - 我错过了吗?
dar*_*iol 12
您应该在路由表中正确构建路由.例如.:
routes.MapRoute("MyRoute" ,"{controler}/{id}#{detail}" ,new { controller = "users", action = "index", id = (string)null, detail = "Summary" });
Run Code Online (Sandbox Code Playgroud)
小智 5
UrlHelper.GenerateUrl包含片段参数.我创建了一个扩展方法
public static string Action(this UrlHelper url, string actionName, string controllerName, string fragment, object routeValues)
{
return UrlHelper.GenerateUrl(
routeName: null,
actionName: actionName,
controllerName: controllerName,
routeValues: new System.Web.Routing.RouteValueDictionary(routeValues),
fragment: fragment,
protocol: null,
hostName: null,
routeCollection: url.RouteCollection,
requestContext: url.RequestContext,
includeImplicitMvcValues: true /*helps fill in the nulls above*/
);
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个RedirectToFragmentResult类
public class RedirectToFragmentResult: RedirectResult
{
public UrlHelper Url {get;set;}
public string Action { get; set; }
public string Controller { get; set; }
public string Fragment { get; set; }
public object RouteValues { get; set; }
public RedirectToFragmentResult(UrlHelper url, string action, string controller, string fragment, object routeValues)
:base(url.Action(action, controller, fragment, routeValues))
{
Url = url;
Action = action;
Controller = controller;
Fragment = fragment;
RouteValues = routeValues;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以在单元测试中创建一个新的RouteValueDictionary(result.RouteValues),以便按照RedirectToRouteResult的方式进行检查.
| 归档时间: |
|
| 查看次数: |
5111 次 |
| 最近记录: |