我的控制器中有这一行:
return Json(new { ok = true, newurl = (@Url.Content("~/Maintenance/_TitleDetails")) }, "application/json", JsonRequestBehavior.DenyGet);
(基于我在我的视图中使用的工作原理。但它将URL加载为
Http://Ip..../~/Maintenance_TitleDetails
并因此失败,因为应该有应用名称,其中〜是。
这适用于视图放置可能是特定剃刀。
对于Html生成的链接也有相同的问题。有人可以指出我正确的方向,这是最好的方式吗?
修改
尝试在C#中捕获上下文,但也失败了。
System.Web.HttpContext curContext = System.Web.HttpContext.Current;
return Json(new { ok = true, newurl = (curContext + ("/Maintenance/_TitleDetails")) }, "application/json", JsonRequestBehavior.DenyGet);
http://162.168.0.1/System.Web.HttpContext/Maintenance/_TitleDetails 404(未找到)
编辑2 适用于
string baseUrl = System.Web.HttpContext.Current.Request.ApplicationPath.TrimEnd('/');
return Json(new { ok = true, newurl = (baseUrl + ("/Maintenance/_TitleDetails")) }, "application/json", JsonRequestBehavior.DenyGet);
但是想知道是否有更好的方法,而不是在每个控制器动作中都有这个?