gpt4 book ai didi

c# - 向所有传入/传出 URL 添加参数

转载 作者:行者123 更新时间:2023-11-30 15:30:15 25 4
gpt4 key购买 nike

我在将 URL 参数添加到每个生成的 URL 或重定向到 ASP MVC 4 应用程序时遇到问题。

我想生成一个 ID,并在整个应用程序的任何时候使用这个 ID。在 session 中存储 id 不是一个选项,因为单个 session 可能同时打开多个浏览器窗口/选项卡(每个具有不同的 id)


路由配置

routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{customId}",
defaults: new { controller = "Home", action = "Index", customid = UrlParameter.Optional }
);

HomeController.cs

public class HomeController : Controller
{
public ActionResult Index()
{
var customId = Guid.NewGuid();

ControllerContext.RequestContext.RouteData.Values.Add("customId", customId);

//How do I get this redirect to add customid to the url?
//E.g. /Home/Start/{customId}
return RedirectToAction("Start");
//I could do this: But I want it this to happen for every URL,
//and I don't want to replicate this code everywhere
//return RedirectToAction("Start", new { customId = customId });
}

public ActionResult Start()
{
object customId;

//Redirect Loop
if (!Request.RequestContext.RouteData.Values.TryGetValue("customId", out customId))
{
//To generate the ID
return RedirectToAction("Index");
}

ViewData["customId"] = Guid.Parse(customId.ToString());

return View();
}

public ActionResult Next()
{
object customId;

//Redirect Loop
if (!Request.RequestContext.RouteData.Values.TryGetValue("customId", out customId))
{
//To generate the ID
return RedirectToAction("Index");
}

ViewData["customId"] = Guid.Parse(customId.ToString());

return View();
}
}

我不仅希望 ID 自动插入任何重定向结果,而且在呈现 View 时 @Url.Action()@Html.ActionLink() 还应该将 ID 添加到生成的 URL。

开始.cshtml

@*Both of these should generate an href="~/Home/Next/{customId}"*@
@Html.ActionLink("Go to Next", "Next", "Home")
<a href="@Url.Action("Next", "Home")">Go to Next</a>

如何在 ASP MVC 中为所有传出路由自动添加 ID?

最佳答案

创建一个将 ID 添加到 OnActionExecuting 方法中的路由数据的 Action 过滤器?您可以通过过滤器上下文(和 viewbag)访问 Controller 。只要您的 viewbag 包含 customId,您就应该能够将其添加到路由数据中。至少这样你只需要记住在 Controller 上添加属性。

创建一个继承自System.Web.Mvc.Controller 的基类并实现您自己的RedirectToAction。然后让你所有的 Controller 继承自 MyControllerBase。像这样。

public class MyControllerBase : Controller
{
public RedirectToRouteResult RedirectToAction<TController>(Expression<Func<TController, object>> actionExpression)
{
var custId = ControllerContext.Controller.ViewBag["customId"];
string controllerName = typeof(TController).GetControllerName();
string actionName = actionExpression.GetActionName();

return RedirectToAction(actionName, controllerName, new {cId = custId});
}
}

第 2 部分:我在每个 View 上修改 URL 的另一种方法(我知道我在某处有代码!),我需要 URL 从移动站点链接到完整的浏览器站点并从数据库中读取映射。所以在我的页脚中,我有以下内容:

<a id="fullSiteLink" href="<%= ViewData[AppConstants.MainSiteUrl] %>">Visit our Full Browser site</a><br />

然后我向基本 Controller 类和 onactionexecuting(在操作之前)添加了一个过滤器,

public void OnActionExecuting(ActionExecutingContext filterContext)
{
var mainSiteUrl = _mobileToMainRedirect.GetMainSiteUrl(filterContext.HttpContext.Request.Url);
filterContext.Controller.ViewData.Add(AppConstants.MainSiteUrl, string.IsNullOrEmpty(mainSiteUrl) ? UrlHelperExtensions.FullBrowserSite(filterContext.HttpContext.Request.Url) : mainSiteUrl);
}

关于c# - 向所有传入/传出 URL 添加参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22492867/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com