Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# MVC响应。重定向不工作_C#_Asp.net_Asp.net Mvc_Controller_Asp.net Ajax - Fatal编程技术网

C# MVC响应。重定向不工作

C# MVC响应。重定向不工作,c#,asp.net,asp.net-mvc,controller,asp.net-ajax,C#,Asp.net,Asp.net Mvc,Controller,Asp.net Ajax,我在MVC控制器中有一个方法,它是从href-inside-anchor标记调用的 public ActionResult DoSomething(string value) { if(true) { return new RedirectResult("http://google.com"); } } 当我调试并点击那个方法响应时。重定向什么都不做,也没有异常。有什么想法吗? 提前谢谢 使用重定向 return Redirect("http://ww

我在MVC控制器中有一个方法,它是从href-inside-anchor标记调用的

public ActionResult DoSomething(string value)
{
    if(true)
    {
        return new RedirectResult("http://google.com");
    }
}

当我调试并点击那个方法响应时。重定向什么都不做,也没有异常。有什么想法吗?
提前谢谢

使用
重定向

return Redirect("http://www.google.com");
Response.Redirect
不是asp.net mvc中执行重定向的首选方法


更新:似乎您正在尝试重定向ajax请求。如果重定向ajax请求,您的主页面将不会被重定向。

使用
redirect

return Redirect("http://www.google.com");
Response.Redirect
不是asp.net mvc中执行重定向的首选方法


更新:似乎您正在尝试重定向ajax请求。如果您重定向ajax请求,您的主页面将不会被重定向。

为了避免所有这些问题,您需要在这里做一些事情

从您遇到的AJAX错误开始,它们最喜欢与javascript调试器相关,Microsoft称之为“BrowserLink”

如果您使用Firefox或Chrome,此功能根本不起作用,这可能是避免此问题的最简单方法,但是您可以在此处禁用此功能:

您可以将默认浏览器更改为仅在左侧运行网站


关于
Response.Redirect
,我认为已经很好地介绍了这一点,您应该使用
return Redirect()
,但是您的代码需要进行重构才能实现这一点

假设该方法是一个辅助方法,需要与控制器本身分离,那么有两种主要的方法来完成您想要做的事情

1)魔法值

这可能包括“
redirect1
”或通常为
null
,看起来像:

public ActionResult MyAction
{
    string data = ProcessString("some data");

    if (data == null) { return Redirect("google.com"); }
}

public string ProcessString(string input)
{
    if (condition) { return null; }
    string output = input + "!"; // or whatever you need to do!
    return input;
}
2)通过异常处理

假设问题是数据在某种程度上是坏的,并且由于无法处理而想要重定向,那么异常处理很可能是解决方法。它还允许通过单个方法引发不同类型的异常,并独立处理,而无需使用无法用作正常数据的神奇值

public ActionResult MyAction
{
    string data; // remember this will be null, not "" by default

    try
    {
        data = ProcessString("some data");
    }
    catch (OwlMisalignedException ex)
    {
        return RedirectToAction("Index", "Error", new { exData = ex.Code });
    }

    // proceed with controller as normal
}

public string ProcessString(string input)
{
    if (condition) 
    {
        throw new OwlMisalignedException(1234);
        // this is (obviously) a made up exception with a Code property 
        // as an example of passing the error code back up to an error 
        // handling page, for example.
    }

    string output = input + "!"; // or whatever you need to do!
    return input;
}
通过使用这种方法,您可以有效地向方法添加额外的返回状态,而无需修改返回类型或创建大量的神奇值

不要使用
抛出异常
-或者使用更具体的类型之一
ArgumentException
ArgumentNullException
,或者根据需要创建自己的类型


您可以在此处轻松找到有关创建自己的异常类型的信息。

为了避免所有这些问题,您需要在此处执行一些操作

从您遇到的AJAX错误开始,它们最喜欢与javascript调试器相关,Microsoft称之为“BrowserLink”

如果您使用Firefox或Chrome,此功能根本不起作用,这可能是避免此问题的最简单方法,但是您可以在此处禁用此功能:

您可以将默认浏览器更改为仅在左侧运行网站


关于
Response.Redirect
,我认为已经很好地介绍了这一点,您应该使用
return Redirect()
,但是您的代码需要进行重构才能实现这一点

假设该方法是一个辅助方法,需要与控制器本身分离,那么有两种主要的方法来完成您想要做的事情

1)魔法值

这可能包括“
redirect1
”或通常为
null
,看起来像:

public ActionResult MyAction
{
    string data = ProcessString("some data");

    if (data == null) { return Redirect("google.com"); }
}

public string ProcessString(string input)
{
    if (condition) { return null; }
    string output = input + "!"; // or whatever you need to do!
    return input;
}
2)通过异常处理

假设问题是数据在某种程度上是坏的,并且由于无法处理而想要重定向,那么异常处理很可能是解决方法。它还允许通过单个方法引发不同类型的异常,并独立处理,而无需使用无法用作正常数据的神奇值

public ActionResult MyAction
{
    string data; // remember this will be null, not "" by default

    try
    {
        data = ProcessString("some data");
    }
    catch (OwlMisalignedException ex)
    {
        return RedirectToAction("Index", "Error", new { exData = ex.Code });
    }

    // proceed with controller as normal
}

public string ProcessString(string input)
{
    if (condition) 
    {
        throw new OwlMisalignedException(1234);
        // this is (obviously) a made up exception with a Code property 
        // as an example of passing the error code back up to an error 
        // handling page, for example.
    }

    string output = input + "!"; // or whatever you need to do!
    return input;
}
通过使用这种方法,您可以有效地向方法添加额外的返回状态,而无需修改返回类型或创建大量的神奇值

不要使用
抛出异常
-或者使用更具体的类型之一
ArgumentException
ArgumentNullException
,或者根据需要创建自己的类型


您可以很容易地在此处找到有关创建自己的异常类型的信息。

我可能认为这会起作用,但通常您会在控制器内使用一个
return Redirect
方法从控制器重定向。在浏览器调试器XMLHttpRequest中发现错误,无法加载。请求的资源上不存在“Access Control Allow Origin”标头。因此不允许访问源代码“”。这很奇怪,这是为防止未经授权的跨站点AJAX请求而添加的安全措施-不确定响应的原因。从服务器端重定向会导致这种情况!!这是意料之中的。Ajax请求被重定向到第三方站点(本例中为google),并被浏览器失败,因为google未指定允许来自此站点的Ajax请求的标题。这个问题缺乏一些细节。你想做什么?服务器端重定向不是AJAX请求,但我刚刚想起了为什么-在调试模式下,Visual Studio将JavaScript调试帮助程序注入页面,然后在重定向到Google时将其阻止。我可能预计这会起作用,但通常情况下,您会使用控制器中的一个
return Redirect
方法从控制器重定向。在浏览器调试器XMLHttpRequest中发现错误,无法加载。请求的资源上不存在“Access Control Allow Origin”标头。因此不允许访问源代码“”。这很奇怪,这就是securit