簡體   English   中英

ASP.NET MVC 3中的RestFul操作方法

[英]RestFul action methods in ASP.NET MVC 3

我無法在我的action方法內檢索我的POST請求正文。

我使用提琴手來發起發布請求。

這是我正在使用的控制器。

public class EventController : Controller
{
    //
    // GET: /Event/

    public ActionResult Index()
    {
        return View();
    }


    [HttpPost]
    [ActionName("Event")]
    public String AddSurvey()
    {
        // ... logic to modify or create data item

        return "Ahmed" + Request["person"];
    }
}

這就是我的global.asax的配置方式

 public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


        routes.MapRoute(null, "Event",
             new { controller = "Event", action = "Event" });

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );



    }

當我發起來自提琴手的POST請求時,該方法上的斷點將被調用。 但是,當我觀看Request [“ person”]的值時,其中沒有null。

它是global.asax中定義的路由嗎? 請幫忙

嘗試將Index操作重命名為Event [ActionName("Event")]表示您的Get動作稱為Event

當某人在常規ASP.NET MVC中尋找RESTful方法時,對我來說重要的是要確保他知道完全符合此需求WebAPI

ASP.NET Web API是一個框架,可輕松構建可訪問包括瀏覽器和移動設備在內的廣泛客戶端的HTTP服務。 ASP.NET Web API是在.NET Framework上構建RESTful應用程序的理想平台。

我不知道,怎么了。 還是為什么。 但這有效

  [HttpPost]
    [ActionName("Event")]
    public String AddSurvey()
    {
        // ... logic to modify or create data item
        string body;
        using (StreamReader reader = new StreamReader(Request.InputStream))
        {
            body = reader.ReadToEnd();
            reader.Close();
        }


        return body;
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM