首页 / C# / c#-将参数添加到所有传入/传出URL
c#-将参数添加到所有传入/传出URL
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#-将参数添加到所有传入/传出URL,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3236字,纯文字阅读大概需要5分钟。
内容图文
![c#-将参数添加到所有传入/传出URL](/upload/InfoBanner/zyjiaocheng/679/b1c309f5074a47098b95927608ee32df.jpg)
我在将URL参数添加到生成的每个URL或在ASP MVC 4应用程序中重定向到每个URL时遇到麻烦.
我想生成一个ID,并在整个应用程序中的任何时候使用此ID.不能将ID存储在会话中,因为单个会话可能会同时打开多个浏览器窗口/标签(每个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自动插入任何重定向结果中,而且在呈现视图时,@ 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>
如何将ID自动添加到ASP MVC中的所有传出路由?
解决方法:
创建一个动作过滤器,该动作过滤器会将ID添加到OnActionExecuting方法中的路线数据中?您可以通过过滤器上下文(和视图包)访问控制器.只要您的视图包包含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});
}
}
第2部分:
在每个视图上修改URL的另一种方法(我知道我的代码在哪里!),我需要将该URL从移动网站链接到完整的浏览器网站,并从数据库中读取映射.因此,在页脚中,我具有以下内容:
<a id="fullSiteLink" href="<%= ViewData[AppConstants.MainSiteUrl] %>">Visit our Full Browser site</a><br />
然后,我向基本控制器类添加了一个过滤器,并在操作之前添加了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全部内容,希望文章能够帮你解决c#-将参数添加到所有传入/传出URL所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。