直接在mvc控制器中添加url

时间:2015-07-14 11:21:35

标签: json html5 asp.net-mvc-5

自从部署我的应用程序后出现问题。很多页面都没有加载,因为如果我配置错误,我就错了。

我的控制器中有这一行:

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);

但是想知道是否有更好的方法,而不是在每个控制器动作中都有这个?

0 个答案:

没有答案