I keep getting a 404 and cannot see why?
GLOBAL.ASAX:
protected void Application_Start(object sender, EventArgs e)
{
// GetApi - Allows for:
// - GET: With or without an Id (because id is marked as 'optional' in defaults)
RouteTable.Routes.MapHttpRoute(name: "GetApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional });
// ActionsApi - Allows for:
// - CREATE
// - DELETE
// - SAVE
// - UPDATE
// - LIST
// - FIND
// - and many, many more
RouteTable.Routes.MapHttpRoute(name: "ActionsApi",
routeTemplate: "api/{controller}/actions/{action}",
defaults: new { });
// QueryByNameApi - Allows for:
// - FIND: By-Name (within the URL...not the data)
RouteTable.Routes.MapHttpRoute(name: "QueryByNameApi",
routeTemplate: "api/{controller}/actions/by-name/{value}",
defaults: new
{
value = "",
action = "QueryByName"
});
}
CONTROLLER:
public class SearchController : ApiController
{
internal const string MSG_UNMANAGEDEXCEPTION = "An unexpected error occurred. Please try again.";
// THIS WORKS !!!
[HttpGet]
public HttpResponseMessage Hello()
{
return Request.CreateResponse(HttpStatusCode.OK, "Hello back!");
}
// BUT...THIS FAILS ???
[HttpPost]
public HttpResponseMessage Find(string value)
{
var result = new SearchResult();
try
{
var term = value.ToLowerInvariant().Trim();
var query = Mock.Categories();
// WHERE
query = query.Where(x => x.Name.ToLowerInvariant().Trim().Contains(term)
|| x.categoryType.Name.ToLowerInvariant().Trim().Contains(term));
// ORDER BY
query = query.OrderBy(x => x.Name)
.ThenBy(x => x.categoryType.Name);
// MATERIALIZED
var collection = query.ToList();
result.Filters = collection.Select(x => x.categoryType).ToList();
result.Records = collection;
}
catch (Exception ex)
{
HttpError error = new HttpError(MSG_UNMANAGEDEXCEPTION);
return Request.CreateResponse(HttpStatusCode.InternalServerError, error);
}
return Request.CreateResponse(HttpStatusCode.OK, result);
}
}
JAVASCRIPT:
The POST fails...
$.ajax({
type: 'POST',
data: { "value": text },
url: 'api/search/actions/find',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
})
But the GET succeeds?...
$.ajax({
type: 'GET',
data: {},
url: 'api/search/actions/hello',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
});
Try replacing by-name
in the routeTemplate
of QueryByNameApi
with {action}
and in SearchController
add [Route("api/search/actions/find/{value:string}")]
attribute to the Find()
method
If you want to match ActionsApi
try executing this ajax call:
$.ajax({
type: 'POST',
url: 'api/search/actions/find/' + value,
contentType: 'application/x-www-form-urlencoded; charset=UTF-8'
})
else if you want to match QueryByNameApi
try:
$.ajax({ type: 'POST', url: 'api/search/actions/find/' + value, contentType: 'application/x-www-form-urlencoded; charset=UTF-8' })
You appear to be sending json data in your post however you have set your content type to application/x-www-form-urlencoded
. I would define a new class to hold your post values and redefine your post action.
public class PostData
{
public string value { get; set; }
}
Controller Action:
[HttpPost]
public HttpResponseMessage Find(PostData Data)
{
string term = Data.value;
//... implementation excluded
}
Then change your ajax call.
$.ajax({
type: 'POST',
data: { "value": text },
url: 'api/search/actions/find',
contentType: 'application/json'
})
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.