I want that in my action method, my Request.Url.Host should be available. However I don't want to set it in routedata because it will be visible to the user in the url. How can I accomplish this such that Request.Url.Host is available in my domain name variabele but now shown to the user in url? I tried adding it to Request.Form.Add("domain",Request.Url.Host) but it doesn't work because Request.Form is readonly collection.
我想要在action方法中,也就是request。url中。主持人应该是可用的。但是,我不希望在routedata中设置它,因为用户可以在url中看到它。如何完成这个请求。url。主机在我的域名中是可用的,但是现在显示给用户在url?我尝试将它添加到Request. form . add(“domain”、Request. url . host)中,但由于请求而无法工作。形式是只读的。
eg:
例如:
public ActionResult Foo(string domain, int id) //id is the standard routedata that you get from url.
{
..//code
}
What is the best I can do? I don't want to do Request.Url.Host in every action method so I am trying this approach.
我能做的最好的事是什么?我不想做request。url。在每个动作方法中都有主机,所以我正在尝试这种方法。
I tried to do this:
我试着这么做:
public class MyRoute : Route
{
#region '----- Method(s) -----'
public MyRoute(string url, object defaults)
: base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
{ }
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData routeData = base.GetRouteData(httpContext);
string domain = httpContext.Request.Url.Host;
routeData.DataTokens["domain"] = domain;
return routeData;
}
#endregion
}
But my domain variable in my action method comes null.
但是我的action方法中的定义域变量是空的。
Thanks.
谢谢。
1 个解决方案
#1
1
EDIT: Complete rewrite:
编辑:完全重写:
You should add your custom data to routeData.Values
instead of routeData.DataTokens
.
您应该向routeData添加自定义数据。值而不是routeData.DataTokens。
public class MyRoute : Route
{
#region '----- Method(s) -----'
public MyRoute(string url, object defaults)
: base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
{ }
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData routeData = base.GetRouteData(httpContext);
string domain = httpContext.Request.Url.Host;
routeData.Values["domain"] = domain;
return routeData;
}
#endregion
}
And registering your MyRoute
route (MVC uses the first matching route so it should go first):
注册你的MyRoute (MVC使用第一个匹配的路径,所以应该先去):
routes.Add(new MyRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));
#1
1
EDIT: Complete rewrite:
编辑:完全重写:
You should add your custom data to routeData.Values
instead of routeData.DataTokens
.
您应该向routeData添加自定义数据。值而不是routeData.DataTokens。
public class MyRoute : Route
{
#region '----- Method(s) -----'
public MyRoute(string url, object defaults)
: base(url, new RouteValueDictionary(defaults), new MvcRouteHandler())
{ }
public override RouteData GetRouteData(HttpContextBase httpContext)
{
RouteData routeData = base.GetRouteData(httpContext);
string domain = httpContext.Request.Url.Host;
routeData.Values["domain"] = domain;
return routeData;
}
#endregion
}
And registering your MyRoute
route (MVC uses the first matching route so it should go first):
注册你的MyRoute (MVC使用第一个匹配的路径,所以应该先去):
routes.Add(new MyRoute("{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }));