首页 > 解决方案 > 子域内的 ASP.NET MVC 应用程序中的表单数据始终为空

问题描述

我有两个 Asp.Net MVC 网站。第一个在根域domain.com内,第二个是子域sub.domain.com

在根域内一切正常,但我无法将表单提交到子域上的控制器。无论我试图发送什么似乎永远不会到达控制器。

这可能看起来很奇怪,但是当我尝试以调试模式在本地启动应用程序时不会发生此问题。(本地不是子域,只是普通的本地主机)。

仅出于测试目的,我建立了一个简单的模型:

public class TestModel
{
    public string test_text { get; set; }
}

这是 CSHTML 部分视图:

@using (Html.BeginForm("TryModel", "Home", new { }, FormMethod.Post, new { }))
{
    <input type="text" name="test_text" />
    <button type="submit">Test</button>
}

即使使用这个简单的配置,在我的本地副本上我也没有问题,但是一旦我在远程机器上启动应用程序,发布的数据总是空的。

你认为可能是什么问题?

编辑

这是控制器方法

    [HttpGet]
    public ActionResult TryModel()
    {
        return View();
    }

    [HttpPost]
    public ActionResult TryModel(TestModel model)
    {
        // code removed

        return View(model);
    }

是的,该动作被正确调用。甚至 Request.Form 也是空的。

标签: asp.net-mvcsubdomain

解决方案


Html.BeginForm 只需要 4 个参数,

参数:字符串 actionName字符串 controllerNameFormMethod 方法对象 htmlAttributes

你应该改变你的参数:

@using (Html.BeginForm("TryModel", "Home", new { }, FormMethod.Post, new { }))

至:

@using (Html.BeginForm("TryModel", "Home", FormMethod.Post, new { }))

我希望它会有所帮助。


推荐阅读