url: 'api/address/PutAddress',
"also tried without PutAddress"
type: 'PUT',
data: JSON.stringify(mdata),
"tried with dataType: 'json' "
success: function
() {
$("#lblinfo").show();
}
});
})
});
The 404 error has this message in the body of the network capture
"{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53284/api/address/PutAddres'.","MessageDetail":"No
action was found on the controller 'Address' that matches the request."}"
I have checked Web.config and it has GET,POST,PUT,DELETE in the handler section
Sorry for the long response, unreliable internet service in the country I was in.
I tried your sugestions and still no change, however I'm wondering if the implementation is wrong.
I have noticed that most examples of PUT seem to use get data by int Id before update, I have used string I'm not sure how to get Id by name.
I may be wrong perhaps I need to add a find address and get the Id in the controller method before doing the update, but I thought EF could do that under the cover.
POST api/<controller>
Public Function PostValue(ByVal EmailMessage As EmailClass) As HttpResponseMessage
End function
The emailClass as the following signature. The signatures matches exactly to what is passed from the Page (jquery).
Public Class EmailClass
<Required(ErrorMessage:="Name is required!")>
Public Property Name As String
<Required(ErrorMessage:="Email is Required!")> _
<RegularExpression("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*", ErrorMessage:="Invalid email address")> _
Public Property EmailFrom As String
Public Property Subject As String
Public Property Body As String
Public Property RtnID As Integer
Public Property EmailTo As String
Public Property EmailToName As String
Public Property Newsletter As Boolean
Public Property HaveAChild As Boolean
Public Property URL As String
End Class
NOTE: My Controller is in a directory off the root called api.
Tam818
0 Points
6 Posts
Web api PUT with 404 error
Jan 06, 2013 01:54 PM|LINK
Hi,
I wonder if anyone can help I have been trying out web api on web forms so far everything has worked except for PUT action.
here is the controller code I'm trying.
public HttpResponseMessage PutAddress(string Postcode, Address address)
{
if (ModelState.IsValid && Postcode == address.Postcode)
{
db.Entry(address).State =EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
}
and the client code is as follows:
$(function() {
$("#btnupdate").click(function() {
var mdata = {
Postcode: $("#TBpostcode").val(),
Street: $("#TBstreet").val()
}
$.ajax({
url: 'api/address/PutAddress', "also tried without PutAddress"
type: 'PUT',
data: JSON.stringify(mdata),
"tried with dataType: 'json' "
success: function () {
$("#lblinfo").show();
}
});
})
});
The 404 error has this message in the body of the network capture
"{"Message":"No HTTP resource was found that matches the request URI 'http://localhost:53284/api/address/PutAddres'.","MessageDetail":"No action was found on the controller 'Address' that matches the request."}"
I have checked Web.config and it has GET,POST,PUT,DELETE in the handler section
Thanks
stockcer
Member
498 Points
128 Posts
Re: Web api PUT with 404 error
Jan 06, 2013 07:06 PM|LINK
ASP.NET Arvixe Liaison
James River Webs, Inc.
Tam818
0 Points
6 Posts
Re: Web api PUT with 404 error
Jan 07, 2013 07:23 AM|LINK
Hello,
Below is the routing table I've been using.
<%@ Import Namespace="System.Web.Http" %>
<%@ Import Namespace="System.Web.Routing" %>
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
Thanks
stockcer
Member
498 Points
128 Posts
Re: Web api PUT with 404 error
Jan 07, 2013 12:30 PM|LINK
Remove the PutAddress from the url.
It should say url: 'api/address', "also tried without PutAddress"
Also...this should not effect anything put you can put a attribute around your function ..
<HttpPut> _
Public Function......
Let me know..
ASP.NET Arvixe Liaison
James River Webs, Inc.
Tam818
0 Points
6 Posts
Re: Web api PUT with 404 error
Jan 08, 2013 09:20 AM|LINK
I have tried it and unfortunately I'm still getting the same error.
stockcer
Member
498 Points
128 Posts
Re: Web api PUT with 404 error
Jan 14, 2013 07:22 PM|LINK
Apologize for not getting back to you sooner. See if this works by adding a reference to your PUT command in your web.config.
<modules runAllManagedModulesForAllRequests="true">
<remove name="UrlRoutingModule"/>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<remove name="WebDAVModule"/>
</modules>
<handlers>
<add name="UrlRoutingHandler" preCondition="integratedMode" verb="*" path="UrlRouting.axd" type="System.Web.HttpForbiddenHandler, System.Web,Version=2.0.0.0, Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"/>
<remove name="WebDAV"/>
<remove name="ExtensionlessUrl-Integrated-4.0"/>
<add name="ExtensionlessUrl-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,DELETE,PUT" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
</handlers>
ASP.NET Arvixe Liaison
James River Webs, Inc.
stockcer
Member
498 Points
128 Posts
Re: Web api PUT with 404 error
Jan 14, 2013 07:24 PM|LINK
also try this; (without the /{api} and place it before the one below
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
ASP.NET Arvixe Liaison
James River Webs, Inc.
Tam818
0 Points
6 Posts
Re: Web api PUT with 404 error
Feb 25, 2013 05:09 AM|LINK
Sorry for the long response, unreliable internet service in the country I was in.
I tried your sugestions and still no change, however I'm wondering if the implementation is wrong.
I have noticed that most examples of PUT seem to use get data by int Id before update, I have used string I'm not sure how to get Id by name.
I may be wrong perhaps I need to add a find address and get the Id in the controller method before doing the update, but I thought EF could do that under the cover.
Any thoughts
stockcer
Member
498 Points
128 Posts
Re: Web api PUT with 404 error
Feb 25, 2013 02:02 PM|LINK
After looking at yours and comparing it to the way I implement, here are a few suggestions.
var dataString = { Name: $("#txtname").val(), EmailFrom: $("#txtemail").val(), Subject: $("#ddlSubject option:selected").text(), Body: $("#txtmessage").val(), Newsletter:$('#chkIAgree').attr('checked')?true:false, HaveAChild: $('#chkchild').attr('checked')?true:false, URL: '' } $.ajax({ url: "/api/Contact", data: JSON.stringify(dataString), type: "POST", contentType: "application/json;charset=utf-8", statusCode: { 201: function (newMovie) { success(newMovie); }, 400: function (xhr) { var errors = JSON.parse(xhr.responseText); fail(errors); } } });then in my Controller I use the following;
POST api/<controller> Public Function PostValue(ByVal EmailMessage As EmailClass) As HttpResponseMessage End functionThe emailClass as the following signature. The signatures matches exactly to what is passed from the Page (jquery).
Public Class EmailClass <Required(ErrorMessage:="Name is required!")> Public Property Name As String <Required(ErrorMessage:="Email is Required!")> _ <RegularExpression("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*([,;]\s*\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)*", ErrorMessage:="Invalid email address")> _ Public Property EmailFrom As String Public Property Subject As String Public Property Body As String Public Property RtnID As Integer Public Property EmailTo As String Public Property EmailToName As String Public Property Newsletter As Boolean Public Property HaveAChild As Boolean Public Property URL As String End ClassNOTE: My Controller is in a directory off the root called api.
ASP.NET Arvixe Liaison
James River Webs, Inc.