我有以下路线:
routes.MapRoute("Event Overview", "{city}/{type}/{id}",
new {city="LA", controller = "BaseEvent", action = "EventOverview"}, new {city = new CityConstraint()});
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
而且,我网站上的几个链接:
@Html.ActionLink("Make", "EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName(), activeTab = "#scheduleLink", session = eventInfo.Key.EventSchedules[0].SessionId, hall = eventInfo.Key.EventSchedules[0].HallId, client = eventInfo.Key.EventSchedules[0].BasePlace.PremieraClientId}, null)
@Html.ActionLink("Make", "EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName(), activeTab = "#scheduleLink", }, null)
这是"事件概述操作:
public ActionResult EventOverview(int id, string type, string activeTab,string hall, string session, string client, string count)
{
var model = CreateEventViewData<EventViewData>(id, type);
model.ActiveTab = activeTab;
model.ScheduleCount = count;
model.SessionId = session;
model.HallId = hall;
model.ClientId = client;
return View("Controls/EventsInfo/EventInfo", model);
}
在第一个链接中传递许多参数,并且所有参数都显示在浏览器的地址字段中:
这是第一个链接:
http://localhost:62291/LA/Film/36?activeTab=%23scheduleLink&session=15&hall=65&client=2&count=1
这是第二个链接:
http://localhost:62291/LA/Film/36?activeTab=%23scheduleLink
我想要这样的东西:
http://localhost:62291/LA/Film/36
隐藏地址行中的参数有哪些方法?
更新:
$(document).ready(function () {
var link = $(".btn_buy_ticket").find("a").click(function (e) {
e.preventDefault();
$.post($(this).attr("href"));
});
})
[HttpPost]
public ActionResult EventOverview(int id) // just for test
{
return RedirectToAction("EventOverview", new {id = id});
}
public ActionResult EventOverview(int id, string type, string activeTab,string hall, string session, string client, string count)
{
var model = CreateEventViewData<EventViewData>(id, type);
model.ActiveTab = activeTab;
model.ScheduleCount = count;
model.SessionId = session;
model.HallId = hall;
model.ClientId = client;
return View("Controls/EventsInfo/EventInfo", model);
}
调用所有操作,但我的EventInfo
视图未加载。
你可以用 POST 代替 GET。因此,您可以将链接替换为包含不希望出现在查询字符串中的参数的隐藏字段的表单:
@using (Html.BeginForm("EventOverview", "BaseEvent", new { id = eventInfo.Key.OID, type = eventInfo.Key.XPObjectType.TypeName.GetShortTypeName() }, FormMethod.Post, null))
{
@Html.Hidden("activeTab", "#scheduleLink")
@Html.Hidden("session", eventInfo.Key.EventSchedules[0].SessionId)
@Html.Hidden("hall", eventInfo.Key.EventSchedules[0].HallId)
@Html.Hidden("client", eventInfo.Key.EventSchedules[0].BasePlace.PremieraClientId)
<button type="submit">Make</button>
}
如何隐藏网址参数
如果要隐藏 URL 参数,请转到调试属性并选择 radiobutton
选项并指定具体页面:
http://localhost:61757/TicketDataUpload/templateupload?projectid=6497&userid=7336
这是参数网址。如果你想像这样隐藏:
http://localhost:61757/Controller/index
您必须放入特定页面,因为当您打开页面时,它不会显示 URL 参数。
我的(有点丑陋的)解决方案:保持代码不变,然后在加载 html 文档后(此时服务器已经使用了您的查询字符串数据),使用 javascript 中的 history.pushState 更改地址栏中的 url,如下所示:
$(document).ready(function () {
let hrefWithoutQueryString = window.location.href.split('?')[0];
// args below are 'state' (irrelevant for me), 'title' (so far,
// ignored by most browsers) and 'url' (will appear in address bar)
history.pushState({ }, '', hrefWithoutQueryString);
});
在一瞬间,查询字符串将出现在地址栏中,但是在上面的js运行之后,url中包括"?"和之后的所有内容都将消失。
显然,副作用是它会更改浏览器的会话历史记录,但这对我来说不是问题。
请注意,我已经将空状态传递到 pushState - 同样,这对我来说不是问题,但可能会导致问题,具体取决于应用程序的路由设置方式以及它是否使用状态变量。