我如何在Razor View .cshtml中重定向页面



@{Response.Redirect("Index","Home")};

上述代码不起作用,请帮助我。

Response.Redirect是服务器端操作,在视图中使用毫无意义。您正在追随适当的客户端操作。

您只需在JavaScript(客户端端)中更改window.location.href即可重定向浏览器,然后使用Url.Action在JavaScript中生成(服务器端)适当的客户端URL。

例如。类似:

<input type="button" onclick="function(){window.location.href=@(Url.Action("Index", Home"))}" value="Press Me!"/>

坦率地说,您可能只需要一个看起来像按钮的链接,因为在这种情况下使用location.href重定向没有优势。

最新更新