[英]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.