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

标签 c# asp.net asp.net-mvc asp.net-mvc-4

我在将 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/

相关文章:

c# - 如何将列表集合添加到列表中?

c# - OpenGL ES 2.0/MonoTouch : Texture is colorized red

c# - 在方法中获取 session 变量

asp.net - 在asp.net中填写Word文档?

asp.net - DataContract IsReference=true 返回空对象

c# - 从集线器外部广播的 SignalR 不起作用

c# - 使用 32Feet 查找配对的蓝牙设备是否在范围内

c# - 代理后面单声道上的 NuGet 无法连接到互联网

c# - 循环直到 TcpClient 响应完全读取

asp.net-mvc - 如何使用 ASP.MVC 4 向 HTML.BeginForm 添加属性?