Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将参数添加到所有传入/传出URL';s_C#_Asp.net_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 将参数添加到所有传入/传出URL';s

C# 将参数添加到所有传入/传出URL';s,c#,asp.net,asp.net-mvc,asp.net-mvc-4,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,在ASP MVC 4应用程序中,向生成的或重定向到的每个URL添加URL参数时遇到问题 我想生成一个ID,并在整个应用程序中的任何时候使用该ID。在会话中存储id不是一个选项,因为单个会话可能会同时打开多个浏览器窗口/选项卡(每个窗口/选项卡具有不同的id) RouteConfig routes.MapRoute( name: "Default", url: "{controller}/{action}/{customId}",

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

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


RouteConfig

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自动插入任何重定向结果,而且在呈现视图时,
@Url.Action()
@Html.ActionLink()
还应将ID添加到生成的Url中

Start.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>
@*这两者都应生成href=“~/Home/Next/{customId}”*@
@ActionLink(“转到下一个”、“下一个”、“主页”)


如何在ASP MVC中自动向所有传出路由添加ID?在黑暗中完成拍摄

您可以设置路由,以便在未提供值的情况下创建Id。这样,如果该值存在,它将使用提供的Id。否则,它将创建一个

由于这是利用路由,因此即使使用以下方法,您也可以生成Id:
@Html.ActionLink(“转到下一个”、“下一个”、“主页”)


注意:您将用自己的Id生成器替换
Guid.NewGuid()

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

创建从System.Web.Mvc.Controller继承的基类,并实现您自己的RedirectToAction。然后让所有控制器从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});
    }
}

这可能会有帮助@garethb是的(正如代码注释中提到的),这确实有效。但是,此ID将在多个控制器和视图中使用,并且必须添加带有
customId
(记住这样做!)的匿名类型是一件痛苦的事情:-)对不起,错过了!:)管线只创建一次。在此处设置默认值将更改整个应用程序的默认值。(如果可能的话)它必须是一个委托,例如
customId=()=>Guid.NewGuid()两者都是不错的选择。最后,我使用了一个全局过滤器在
OnActionExecuted
中添加ID(在操作运行之后,在结果执行之前)。这就解决了
重定向到操作
的问题。还有一部分需要更新传出URL。最后,我通过在
MvcHandler.BeginProcessRequest
中的
RouteData.Values
中插入
customId
来实现这一点,要使其正常工作,您还需要路由中的
customId
。我将发布我的代码并接受以下内容:)如果您可以发布您的代码,那就太好了!我已经添加了过去的方法,但如果能看到另一种解决方案,那将是一件好事!
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});
    }
}
<a id="fullSiteLink" href="<%= ViewData[AppConstants.MainSiteUrl] %>">Visit our Full Browser site</a><br />
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);
}