[英]Error No 'Access-Control-Allow-Origin' header is present on the requested resource while redirecting on Action Filter onActionExecuting
I have an issue in redirecting to the external URL Requirement I want to redirect to an external URL whenever the session is expired. 我在重定向到外部URL时遇到问题要求我希望在会话过期时重定向到外部URL。
I have a separate class in which I override the definition of onActionExecuting Action 我有一个单独的类,我在其中覆盖onActionExecuting Action的定义
Here is my Code: 这是我的代码:
public class SessionExpireAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
// var redirectUrl = "http://www.google.com";
var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();
if (isAjaxRequest)
{
filterContext.Result = new RedirectResult("http://www.google.com");
return;
}
base.OnActionExecuting(filterContext);
}
}
I Have try almost all the solution that I could find on Stack Over Flow but my problem is not still solved. 我已经尝试了几乎所有可以在Stack Over Flow上找到的解决方案,但我的问题仍未解决。
Is there is any issue related to pipeline execution of controller filters if so kindly guide me. 是否有任何与管道执行控制器过滤器有关的问题,如果这样善意指导我。
Note on console its shows this error 在控制台上注意它显示此错误
XMLHttpRequest cannot load http://www.google.com/ .
XMLHttpRequest无法加载http://www.google.com/ 。 No 'Access-Control-Allow-Origin' header is present on the requested resource.
请求的资源上不存在“Access-Control-Allow-Origin”标头。 Origin ' http://localhost:49815 ' is therefore not allowed access.
因此不允许来源' http:// localhost:49815 '访问。
Edited 编辑
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var redirectUrl = "http://www.google.com";
var isAjaxRequest = filterContext.HttpContext.Request.IsAjaxRequest();
if (isAjaxRequest)
{
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary { { "controller", "TestController" }, { "action", "Redirect" } });
if (filterContext.Result!=null)
return;
}
else
{
base.OnActionExecuting(filterContext);
}
}
And This is my Controller & Action 这是我的控制器与行动
public class TestController : Controller
{
public ActionResult Redirect()
{
return Redirect("http://www.google.com");
}
}
Thanks in advance. 提前致谢。
The first thing you are doing an XMLHttpRequest
to a different domain than your page is on. 您首先要将
XMLHttpRequest
发送到与您的页面不同的域。 You are getting the error because of same origin for security reasons . 出于安全原因,您因为相同的来源而收到错误。
Consider using this Cross-Origin XMLHttpRequest 考虑使用此Cross-Origin XMLHttpRequest
Also consider Using CORS 另请考虑使用CORS
Cross-Origin Resource Sharing (CORS) is a W3C spec that allows cross-domain communication from the browser. 跨源资源共享(CORS)是一种允许来自浏览器的跨域通信的W3C规范。 By building on top of the XMLHttpRequest object, CORS allows developers to work with the same idioms as same-domain requests
通过构建在XMLHttpRequest对象之上,CORS允许开发人员使用与同域请求相同的习惯用法
EDIT: 编辑:
As per OP's comment adding this. 根据OP的评论添加此内容。 You redirect to controller action method like below :
您重定向到控制器操作方法,如下所示:
filterContext.Result = new RedirectToRouteResult(
new RouteValueDictionary
{{"controller", "Home"}, {"action", "Index"}});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.