gpt4 book ai didi

ASP.NET MVC 当前请求 url(ajax 调用问题)

转载 作者:行者123 更新时间:2023-12-05 00:41:41 25 4
gpt4 key购买 nike

我使用流行的技术“ReturnUrl”。我将当前页面 url 传递给服务器,做一些工作人员,然后我将用户重定向回这个 url。例如,用户发表评论,然后返回此网址。

@using (Html.BeginForm("AddUserComment", "Home", new { returnUrl = HttpContext.Current.Request.RawUrl }, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
...
}

但是,当我通过 ajax 调用加载此表单时,如下所示:

$.ajax({
type: 'GET',
url: '/Home/ShowUserCommentsBlock/',
data: { entityType: entityType, entityId: entityId },
cache: false,
...
});

HttpContext.Current.Request.RawUrl 返回 ajax 请求 url "/Home/ShowUserCommentsBlock?entityType=...",但我需要当前页面 url,其中 ajax请求被调用。我应该使用什么来代替 HttpContext 对象?

最佳答案

好的,我知道了。我们可以使用Request.UrlReferrer用于获取正确 url 的 ajax 请求的属性,如下所示:

public ActionResult MyActionMethod()
{
if (Request.IsAjaxRequest())
ViewBag.ReturnUrl = HttpContext.Request.UrlReferrer.LocalPath;
else
ViewBag.ReturnUrl = HttpContext.Request.RawUrl;

return View();
}

关于ASP.NET MVC 当前请求 url(ajax 调用问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21538618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com