在 MVC 5 和 Umbraco 中使用 AJAX
Using AJAX with MVC 5 and Umbraco
我需要在局部视图中使用 ajax 来调用 mvc 控制器中的函数来 return 计算。
仅供参考,我正在使用 MVC 5 和 Umbraco 7。
我目前在部分视图中有 ajax 代码(我想在某个时候将其移动到 js 文件)。
这里是 ajax 代码:
function GetTime(name) {
var result = "";
$.ajax({
url: '/TimeDifference/GetTimeDifference',
//url: '@Url.Action("GetTimeDifference", "TimeDifference")',
type: 'GET',
//data: JSON.stringify({ location: name }),
data: ({ location: name }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
cache: false,
success: function (msg) {
result = msg;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
});
return result;
}
这是控制器:
public class TimeDifferenceController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult GetTimeDifference(string location)
{
DateTime utc = DateTime.UtcNow;
string timeZoneName = GetTimeZoneName(location);
TimeZoneInfo gmt = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
TimeZoneInfo local = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
TimeSpan utcOffset = gmt.GetUtcOffset(utc);
TimeSpan localOffset = local.GetUtcOffset(utc);
TimeSpan difference = localOffset - utcOffset;
return Json(Convert.ToInt16(difference.TotalMinutes),JsonRequestBehavior.AllowGet);
}
}
上面的代码给我一个 404 Not Found 错误:
Request URL:http://localhost:100/TimeDifference/GetTimeDifference?location=BVI&_=1511949514552
Request Method:GET
Status Code:404 Not Found
Remote Address:[::1]:100
如果我使用:
url: '@Url.Action("GetTimeDifference", "TimeDifference")'
@Url.Action("GetTimeDifference", "TimeDifference") 为 Null,因此它不会去任何地方。
我也试过:
@Html.Hidden("URLName", Url.Action("GetTimeDifference", "TimeDifference"))
...
url: $("#URLName").val()
Url 仍然是 Null。
我已将条目添加到 Global.asax.cs 中用于路由,即
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "TimeDifference", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
这似乎没有任何作用。
我已经解决了很多之前提出的问题并根据建议进行了修改,但似乎没有任何效果。
由于我是新手,所以我确信它缺少一些非常简单的东西。
非常感谢,
HH
您的控制器不会自动连接,我认为 global.asax.cs 文件也不会起作用。您可以在 Umbraco 启动处理程序中为您的控制器注册自定义路由:https://our.umbraco.org/documentation/reference/routing/custom-routes or you can create your controller as an Umbraco WebApi Controller, which is designed for stuff like this: https://our.umbraco.org/documentation/Reference/Routing/WebApi/.
Umbraco WebAPI 控制器会自动连接,并将 return JSON 或 XML 自动取决于调用客户端的要求。
我需要在局部视图中使用 ajax 来调用 mvc 控制器中的函数来 return 计算。
仅供参考,我正在使用 MVC 5 和 Umbraco 7。
我目前在部分视图中有 ajax 代码(我想在某个时候将其移动到 js 文件)。
这里是 ajax 代码:
function GetTime(name) {
var result = "";
$.ajax({
url: '/TimeDifference/GetTimeDifference',
//url: '@Url.Action("GetTimeDifference", "TimeDifference")',
type: 'GET',
//data: JSON.stringify({ location: name }),
data: ({ location: name }),
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: false,
cache: false,
success: function (msg) {
result = msg;
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
}
});
return result;
}
这是控制器:
public class TimeDifferenceController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpGet]
public JsonResult GetTimeDifference(string location)
{
DateTime utc = DateTime.UtcNow;
string timeZoneName = GetTimeZoneName(location);
TimeZoneInfo gmt = TimeZoneInfo.FindSystemTimeZoneById("GMT Standard Time");
TimeZoneInfo local = TimeZoneInfo.FindSystemTimeZoneById(timeZoneName);
TimeSpan utcOffset = gmt.GetUtcOffset(utc);
TimeSpan localOffset = local.GetUtcOffset(utc);
TimeSpan difference = localOffset - utcOffset;
return Json(Convert.ToInt16(difference.TotalMinutes),JsonRequestBehavior.AllowGet);
}
}
上面的代码给我一个 404 Not Found 错误:
Request URL:http://localhost:100/TimeDifference/GetTimeDifference?location=BVI&_=1511949514552
Request Method:GET
Status Code:404 Not Found
Remote Address:[::1]:100
如果我使用: url: '@Url.Action("GetTimeDifference", "TimeDifference")'
@Url.Action("GetTimeDifference", "TimeDifference") 为 Null,因此它不会去任何地方。
我也试过:
@Html.Hidden("URLName", Url.Action("GetTimeDifference", "TimeDifference"))
...
url: $("#URLName").val()
Url 仍然是 Null。
我已将条目添加到 Global.asax.cs 中用于路由,即
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "TimeDifference", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
这似乎没有任何作用。
我已经解决了很多之前提出的问题并根据建议进行了修改,但似乎没有任何效果。
由于我是新手,所以我确信它缺少一些非常简单的东西。
非常感谢, HH
您的控制器不会自动连接,我认为 global.asax.cs 文件也不会起作用。您可以在 Umbraco 启动处理程序中为您的控制器注册自定义路由:https://our.umbraco.org/documentation/reference/routing/custom-routes or you can create your controller as an Umbraco WebApi Controller, which is designed for stuff like this: https://our.umbraco.org/documentation/Reference/Routing/WebApi/.
Umbraco WebAPI 控制器会自动连接,并将 return JSON 或 XML 自动取决于调用客户端的要求。