路由在ASP.Net保留字 [英] Routing Reserved Words in ASP.Net
问题描述
我有一个旧的URL,我希望映射到一个路由在我的ASP.Net MVC应用程序
I have a legacy url that I wish to map to a route in my ASP.Net MVC application
e.g. http://my.domain.com/article/?action=detail&item=22
现在在路由的创建动作
有特殊的意义,所以我创造这条路呢?该控制器是RedirectController和行动项目。
Now in route creation action
has a special meaning so my to create this route? The controller is a RedirectController and the action is Item.
routes.MapRoute(
name: "Redirect",
url: "article",
defaults:new { controller = "redirect", action = "item"}
);
所以我的问题是,动作
在查询字符串获取由<$ C $的动作
覆盖C>默认。有没有办法来解决这个问题?
So my problem is that action
in the query string gets overwritten by the action
in the defaults
. Is there a way to get around this?
推荐答案
我设法使用自定义的模型绑定器来破解它。我创建一个名为一个基本的类查询字符串
I have managed to crack it using a custom ModelBinder. I create a basic class called QueryString
public class QueryString
{
private readonly IDictionary<string,string> _pairs;
public QueryString()
{
_pairs = new Dictionary<string, string>();
}
public void Add(string key, string value)
{
_pairs.Add(key.ToUpper(), value);
}
public string Get(string key)
{
return _pairs[key.ToUpper()];
}
public bool Contains(string key)
{
return _pairs.ContainsKey(key.ToUpper());
}
}
然后,我创建我的定制绑定为: -
Then I create my custom binder for that:-
public class QueryStringModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var queryString = new QueryString();
var keys = controllerContext.HttpContext.Request.QueryString.AllKeys;
foreach (var key in keys)
{
queryString.Add(key, controllerContext.HttpContext.Request.QueryString[key]);
}
return queryString;
}
}
在我的Global.asax我注册它: -
In my Global.asax I register it:-
ModelBinders.Binders.Add(typeof(QueryString), new QueryStringModelBinder());
现在我可以用在我RedirectController: -
Now I can use that in my RedirectController:-
public RedirectToRouteResult Item(QueryString queryString)
{
// user QueryString object to get what I need
// e.g. queryString.Get("action");
}
这篇关于路由在ASP.Net保留字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!