2014-11-03 51 views
0

我是新來的asp.net,我試圖創建一個API來添加一個字段到我的數據庫,但我越來越此消息:The requested resource does not support http method 'GET'。我真的需要幫助。這裏是我的代碼:Api發佈錯誤:請求的資源不支持http方法'GET'.88

public class PostNewUserController : ApiController 
{ 
    RegisterViewModel newUser=new RegisterViewModel() ; 


    public HttpResponseMessage PostUser(string userName, string password, string confirmPassword) 
    { 
     UsersAdminController us = new UsersAdminController(); 
     newUser.Email = userName; 
     newUser.Password = password; 
     newUser.ConfirmPassword = confirmPassword; 

     var user = new ApplicationUser { UserName = newUser.Email, Email = newUser.Email }; 
     var adminresult = us.UserManager.CreateAsync(user, newUser.Password); 

     var response = Request.CreateResponse<RegisterViewModel>(HttpStatusCode.Created, newUser); 
     return response; 
    } 
} 

這是routeConfig代碼提前

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

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
} 

感謝

+0

http://stackoverflow.com/questions/12765636/the-requested-resource-does-not-support-http-method-get – 2014-11-03 15:06:06

+0

你可以添加你的包含語句到你的問題嗎? – 2014-11-03 15:09:23

回答

0

The requested resource does not support http method 'GET'

該消息意味着你是一個GET請求並沒有在控制器中定義的方法與Word

Get

開始當你讓你的HTTP請求,你必須做出「POST」請求,而不是「GET」請求,因爲你的控制器只有一個POST操作。

1

你應該前綴你的行動像這樣的服務器端:

[HttpPost()] 
[Route()] 
public HttpResponseMessage PostUser(string userName, string password, string confirmPassword) 

編輯: 也應該創建一個包含請求數據而不是通過的模型G中的每個元素,這將是容易當你將需要提供你的行動50個元素,但這是另一回事

相關問題