Please point me to right question if this is a duplicate. I haven't quite found one that addresses what I expressing.
So I have a controller that has two same routes
// GET: api/UserPwordHints/5
[ResponseType(typeof(UserPwordHintsModels))]
public async Task<IHttpActionResult> GetUserPwordHintsModels(string id)
// PUT: api/UserPwordHints/5
[ResponseType(typeof(void))]
public async Task<IHttpActionResult> PutUserPwordHintsModels(string id,
UserPwordHintsModels userPwordHintsModels)
The only difference is the HTTP Method. Now when I use the route, the second route is called
// PUT: api/UserPwordHints/5
My WebConfig.cs looks like this
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
Which is the default. I know through my search on SO that I have add a code that explicitly directs which one to use. However they both have the same route. What would be best way to tell which exact one to use?
EDIT: Using a GET Method still calls the PUT Method with an Error
cannot send a content-body with this verb-type post
EDIT2 Calling Method
GET_METHOD = "GET"
SendWebRequest(getPassWordHintURL, requestBody:=body, httpMethod:=GET_METHOD)
Private Function SendWebRequest(
requestUrl As String,
Optional requestBody As String = Nothing,
Optional httpMethod As String = "POST",
Optional bearerToken As String = Nothing) As String
Dim responseFromServer
httpReq = WebRequest.Create(requestUrl)
httpReq.Proxy = Nothing
httpReq.Method = httpMethod
httpReq.ContentType = "application/json"
If bearerToken IsNot Nothing Then
httpReq.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + bearerToken)
End If
httpReq.ContentLength = 0
If requestBody IsNot Nothing Then
Dim dataBytes = Encoding.UTF8.GetBytes(requestBody)
httpReq.ContentLength = dataBytes.Length
Dim stream = httpReq.GetRequestStream()
stream.Write(dataBytes, 0, dataBytes.Length)
stream.Close()
End If
Dim resp As WebResponse
Try
resp = httpReq.GetResponse()
Dim dataStream = resp.GetResponseStream()
Dim reader = New StreamReader(dataStream)
' Read the content.
responseFromServer = reader.ReadToEnd()
dataStream.Close()
Catch ex As WebException
Throw ex
End Try
Return responseFromServer
End Function
Routes defined for both the methods are same, but type of method is different No need to change default route.
If you want to call GetUserPwordHintsModels()
, then use route as
// GET: api/UserPwordHints/5
^^^^ See the difference
If you want to call PutUserPwordHintsModels()
, then use route as
// PUT: api/UserPwordHints/5
^^^^ PUT and GET
You can decorate your methods using HTTPPut
and HTTPGet
annotations. Similar to
// GET: api/UserPwordHints/5
[ResponseType(typeof(UserPwordHintsModels)), HTTPGet]
public async Task<IHttpActionResult> GetUserPwordHintsModels(string id)
// PUT: api/UserPwordHints/5
[ResponseType(typeof(void)), HTTPPut]
public async Task<IHttpActionResult> PutUserPwordHintsModels(string id,
UserPwordHintsModels userPwordHintsModels)
For more details : MSDN Routing to controller actions
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.