代码之家  ›  专栏  ›  技术社区  ›  JK.

ASP.NET MVC-2如何接收来自其他网站的帖子?

  •  1
  • JK.  · 技术社区  · 14 年前

    例如,另一个站点执行以下操作:

    string url = "https://mvc2-site.com/TestReceive";  // that's my site's controller action
    
    NameValueCollection inputs = new NameValueCollection();
    inputs.Add("SessionId", "11111");
    System.Net.WebClient Client = new WebClient();
    byte[] result = Client.UploadValues(url, inputs);
    

    我如何在我的网站上收到那个帖子?我试过:

    public ActionResult TestReceive(FormCollection fc) {} // doesn't get hit
    
    public ActionResult TestReceive(string SessionId) {} // method gets hit but session id is null
    
    public ActionResult TestReceive() 
    {
        var param = ControllerContext.RouteData.Values["parameter"].ToString(); // Values["parameter"] is null
    } 
    

    我不控制其他网站,它必须是一个职位(不是一个网站服务或其他任何东西)

    作为另一个转折点,它们将发布大约10个变量,但有些变量是可选的,因此它们可能不在POST数据中。

    1 回复  |  直到 14 年前
        1
  •  1
  •   Darin Dimitrov    14 年前
    [HttpPost]
    public ActionResult TestReceive(string sessionId) 
    {
        ...
    }
    

    此外,由于url中没有指定控制器,因此您应该确保包含此操作的控制器是路由中的默认控制器。