c# - Ajax.BeginForm 绝对 url MVC CORS

标签 c# asp.net-mvc jquery asp.net-mvc-4

有没有办法让 Ajax.BeginForm 输出绝对 url 而不仅仅是相对 url?

我正在尝试在另一台服务器上托管的站点上使用 CORS 处理一些表单。 目前,我正在使用 ajax 选项中的 jquery onsuccess 解析表单。

我没有任何关于编写扩展的教程的指针,因为它会很方便

最佳答案

Is there any way to get the Ajax.BeginForm to output a absolute url rather than just a relative one?

是的,但您需要在 AjaxOptions 中指定 Url 并且不使用采取操作和 Controller 名称的重载:

@using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, "http") }))
{
    ...
}

Url.Action 有一个重载,您可以在其中指定要使用的生成绝对 url 的协议(protocol)。如果您想使用与用于请求当前操作的协议(protocol)相同的协议(protocol)而不是对其进行硬编码,您可以使用以下内容:

@using (Ajax.BeginForm(new AjaxOptions { Url = Url.Action("Foo", "Bar", null, Request.Url.Scheme) }))
{
    ...
}

关于c# - Ajax.BeginForm 绝对 url MVC CORS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17965171/

相关文章:

c# - 错误 : 'The system cannot find the file specified.' when trying to access certificate

c# - WPF点击改变按钮背景

c# - 了解互斥锁类

asp.net-mvc - 使用 FormsAuthenticationTicket 存储额外的用户 session 对象 - cookie 大小太大

c# - ASP.NET VNext 调试 MVC 源代码

javascript - 单击时用颜色从中心填充圆形 Div

jquery - 检查所有复选框 Jquery

c# - 从基本 Delphi 窗体派生 C# 窗体

jquery - 如何从 colorbox iframe 重新获取 fullCalendar(jQuery 插件)?

c# - 在 MVC Razor 中访问 WCF 服务