我有一个方法,其输出我将缓存.它需要四个参数; string,string,int,和WindowsIdentity.我需要根据这四个参数创建一个缓存键.最好是:
将它们作为字符串连接在一起并使用该键?
var key = string.Concat(string1, string2, int1.ToString(), identity.ToString());
Run Code Online (Sandbox Code Playgroud)
要么
他们的哈希码是什么?
var key = string1.GetHashCode() ^ string2.GetHashCode() ^ int1.GetHashCode() ^ identity.GetHashCode();
Run Code Online (Sandbox Code Playgroud)
或者是其他东西?有关系吗?在我的特定情况下,这些键只会进入Hashtable(C#v1).
我有一个UserAccountController,它接受这样的路由"/{username}/{action}".
我想创建一些功能,以便我可以将用户带到特定于帐户的页面,而无需事先知道他们的用户名.我希望能够使用URL "/your/{action}"来捕获"你的"作为用户名发送的事实,获取真实用户名(因为他们已登录),并将其重定向到"/他们的实际用户名/ {行动}".
我可以在每个控制器操作中执行此操作,但我宁愿让它发生在之前的某个位置,这将为所有控制器操作执行此操作.我尝试在Controller的Initialize方法中通过更改RouteData.Values["username"]为真实的用户名然后尝试Response.RedirectToRoute(RouteData); Response.End()但这总是把我带到错误的地方(一些完全错误的路线).
更新: 感谢BuildStarted引导我得到这个答案:
public class UserAccountController : Controller
{
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
if ((string) filterContext.RouteData.Values["username"] != "your")
return;
var routeValues = new RouteValueDictionary(filterContext.RouteData.Values);
routeValues["username"] = UserSession.Current.User.Username;
filterContext.Result = new RedirectToRouteResult(routeValues);
}
}
Run Code Online (Sandbox Code Playgroud)