PUT or DELETE verb in ASP.NET MVC on HTML form

后端 未结 3 876
北恋
北恋 2021-01-04 07:42

I have a simple user registration form, with two fields, one for username and another for the password. I have a controller called UserController which has thes

3条回答
  •  醉梦人生
    2021-01-04 08:13

    HTML forms (up to HTML version 4 and XHTML 1) only support GET and POST as HTTP request methods. XHTML 2.0 will support GET, POST, PUT and DELETE for forms.

    A workaround for this for methods through POST by using a hidden form field which is read by the server and dispatch accordingly.

    For now, you may consider using [HttpPost] now or use XmlHttpRequest to use Put verb on your request.


    UPDATE

    You may use SimplyRestfulRouteHandler from MvcContrib

    It quite simple, register this on RegisterRoutes

    public static void RegisterRoutes(RouteCollection routes)
    {
         SimplyRestfulRouteHandler.BuildRoutes(routes);
    }
    

    Add a hidden field like this with name _method inside your form

    
    

    This would be good to go.

提交回复
热议问题