I understand that this question has been asked several times already and I have looked at a lot of the questions, but none of the answers have worked for me thus far.
I am working with an ASP.Net WebAPI 2 Controller using .Net 4.5.2 and posting using Ajax.
I immediately get a 404 error, however looking at insights it shows that there are x amount of requests to that controller and that action\method but its returning a 404 immediately.
This is the the method, and would be grateful if anyone could help me.
[System.Web.Mvc.HttpPost()]
public int InsertRecord(Models.Person model)
{
if (ModelState.IsValid)
{
var person = new Person
{
FirstName = model.FirstName,
LastName = model.LastName,
DateOfBirth = Convert.ToDateTime(model.DateOfBirth)
};
_dbContext.People.Add(person);
_dbContext.SaveChanges();
var id = person.P_id;
return id;
}
else
{
throw new Exception("Invalid Data", null);
}
}
which is being called from my Ajax as follows;
$("#btnSave").click(function () {
$('#FirstName').valid();
$('#LastName').valid();
$('#DateOfBirth').valid();
if ($('#FirstName').valid() &&
$('#LastName').valid() &&
$('#DateOfBirth').valid()) {
//console.log("save clicked");
var jsonData = {
firstName: $('#FirstName').val(),
lastName: $('#LastName').val(),
dateofBirth: $('#DateOfBirth').val()
}
$.ajax(
{
type: "POST", //HTTP POST Method
url: "http://localhost:56658/AddPerson/InsertRecord", // Controller/View
data: jsonData,
success: function (response) {
if (response != null && response.success) {
alert(response.responseText);
} else {
// DoSomethingElse()
alert(response.responseText);
}
},
error: function (response) {
alert("error!"); //
console.log(response);
}
});
return false;
}
});
Edit
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 }
);
routes.MapRoute(
name: "AddPerson",
url: "{controller}/{action}",
defaults: new { controller = "AddPerson", action = "InsertRecord"}
);
}
}
Edit 2
This is the global asax file contents
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
Edit 3
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
As this is suppose to be a Wep API it should try to follow that design intent
Check for WebApiConfic where there is usually
public static class WebApiConfig {
public static void Register(HttpConfiguration config) {
// Attribute routing.
config.MapHttpAttributeRoutes();
// Convention-based routing.
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Notice the api prefix of the default route. This configures how web API endpoints will be called. The api prefix is to avoid route conflicts with the normal MVC framework.
Url would end looking like api/AddPerson
next make sure controller is create properly and follows suggested design. Try to avoid adding too many responsibilities to the controller. Controllers are a UI concern and should be as lean as possible.
public interface IAddPersonCommand {
int? InsertRecord(Models.Person model model);
}
public class PersonController : ApiController {
private readonly IAddPersonCommand service;
public PersonController(IAddPersonCommand service) {
this.service = service;
}
[HttpPost]
public IHttpActionResult Post([FromBody]Models.Person model) {
if (ModelState.IsValid) {
var id = service.InsertRecord(model);
if(id !=null) {
return Ok(id);
}
}
//If we get this far bad request
return BadRequest();
}
}
Related
I have a route defined in an area, have set the configuration up and routes are firing as expected, however, I can hit the area routes on the root profile as well.
Is there a way to block the root profile from seeing my area routes (defined only in the area only), something like this:
/area/api/awesomeservice -> only this one should be allowed
/api/awesomeservice -> should not be allowed
RouteConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "TestBed.Controllers" }
);
}
}
WebApiConfig.cs:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
Service controller under /Areas/Services
ServiceController.cs
namespace TestBed.Areas.Services.Controllers
{
public class HotFuzzController : ApiController
{
[HttpGet]
public string Get()
{
return "Hello World";
}
}
}
Area configuration for Areas/Services
ServicesAreaRegistration.cs
namespace TestBed.Areas.Services
{
public class ServicesAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Services";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.Routes.MapHttpRoute(
name: "ServicesApiAction",
routeTemplate: "services/api/{controller}/{action}");
context.Routes.MapHttpRoute(
name: "ServicesApi",
routeTemplate: "services/api/{controller}");
context.MapRoute(
"Services_default",
"Services/{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "TestBed.Areas.Services.Controllers" }
);
}
}
}
Global.asax.cs
namespace TestBed
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
The problem comes from the default route that matches the areas controllers. Unfortunately there is no way in the framework (that I know of) to make a route only for controllers that are not in an area.
But you can make a custom RouteContraint and apply your own matching rules. Here is an example of a route constraint that uses reflection to match only controllers of the executing assembly that are in namespaces :
public class NamespaceFilterConstraint : System.Web.Http.Routing.IHttpRouteConstraint
{
private Type[] types;
public string[] AllowedNamespaces { get; }
public NamespaceFilterConstraint(string[] ns)
{
AllowedNamespaces = ns;
types = Assembly.GetExecutingAssembly().GetTypes();
}
public bool Match(HttpRequestMessage request, IHttpRoute route, string parameterName, IDictionary<string, object> values, HttpRouteDirection routeDirection)
{
var controllerName = values["controller"] + "Controller";
//This assumes no controllers with the same name in different namespaces
Type controllerType = types.FirstOrDefault(t => t.Name == controllerName);
return AllowedNamespaces.Contains(controllerType.Namespace);
}
}
You can use it in the WebApiConfig :
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional },
constraints: new { controller = new NamespaceFilterConstraint(new string[] { "MyApp.Controllers" }) }
);
I am getting really weird routing issues when calling actions in my APIController. I have a WebApp, but needed an APIController so I added that as well as WebAPIConfig.cs to App_start and Global.asax.
However, when I try to call different Actions inside the APIController, it seems to not differentiate between the Actions unless I add a parameter. For example, if I call api/controller/happy it enters the same Action as api/controller/sad. It enters the Action that was created first in the Controller.
It makes no sense to me, the Action-names are not being considered in the URL.
my API Controller:
public class RegistrationManagerController : ApiController
{
EventHelper eh = new EventHelper();
[HttpGet]
public IHttpActionResult IsUserRegistered(string skypeid)
{
var skypeuser = Exist.CheckIfRegistered(skypeid);
return Ok(skypeuser);
}
[HttpGet]
public async Task<IHttpActionResult> Happy()
{
var events = await eh.GetHappyRecent();
return Ok(events);
}
[HttpGet]
public async Task<IHttpActionResult> Sad()
{
var events = await eh.GetSadRecent();
return Ok(events);
}
[HttpPost]
public async Task<IHttpActionResult> UpdateEvent() //TODO id send in body?
{
await eh.Update("id");
return Ok();
}
}
My WebAPIConfig.cs:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
My RouteConfig (This is a webapp, not a web API):
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();// New feature
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Routetemplate should be
routeTemplate: "api/{controller}/{action}/{id}"
WebApi follows REST principals so it doesn't route the same way an MVC Controller routes.
Check out this answer here I think it will help you
need route for my web api 2 controller
I'm developing a web application using ASP.NET Web Api and angularJs.
I have a web api controller like this:
[ActionName("AddNewState")]
public object PostAddNewState(RegionInformation newStateParam)
{
RegionOperations regionOperation = new RegionOperations(newStateParam.StateName);
RegionInformation newStateInformation = regionOperation.NewStateInformation;
var text = new
{
newStateInformation
};
return JsonConvert.SerializeObject(text);
}
[ActionName("AddNewCity")]
public object PostAddNewCity(RegionInformation newCityParam)
{
var text = new
{
message="Hello"
};
return JsonConvert.SerializeObject(text);
}
and in client side I have these functions for sending POST request:
$scope.AddNewState = function () {
$http({
method: "POST",
url: "api/RegionManagement/AddNewState",
data: {
StateName: $scope.state
}
}).then(function (response) {
var obj = JSON.parse(response.data);
$scope.States.push({ text: obj.newStateInformation.StateName, value: obj.newStateInformation.ID });
});
};
$scope.AddNewCity = function () {
$http({
method: "POST",
url: "api/RegionManagement/AddNewCity",
data: {
ParentID: $scope.RegionInptes.ParentID,
CityName: $scope.city
}
}).then(function (response) {
var obj = JSON.parse(response.data);
alert(obj.message);
});
};
When I execute $scope.AddNewCity or $scope.AddNewState I face with 500 Internal Server Error.if I comment AddNewCity action in web api controller then I can execute $scope.AddNewState successfully.
I searched for using multiple HTTPPost in a web api controller and try this solution: Multiple HttpPost method in Web API controller, but nothing happened and I still have that error.
UPDATE
This is my config file:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ControllerOnly",
routeTemplate: "api/{controller}"
);
config.Routes.MapHttpRoute(
name: "ControllerandId",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}"
);
}
The order in which you register your routes is important. register more specific routes first and the more general routes after.
public static void Register(HttpConfiguration config) {
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "api/{controller}/{action}/{id}"
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "ControllerandId",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
You should also update the controller to be a little more specific as to what it can handle.
public class RegionManagementController : ApiController {
[HttpPost]
[ActionName("AddNewState")]
public object PostAddNewState(RegionInformation newStateParam) { ... }
[HttpPost]
[ActionName("AddNewCity")]
public object PostAddNewCity(RegionInformation newCityParam) { ... }
}
May be this is because of the number of parameters in the request url
[ActionName("AddNewState")]
public object PostAddNewState(RegionInformation newStateParam)
{
RegionOperations regionOperation = new RegionOperations(newStateParam.StateName);
RegionInformation newStateInformation = regionOperation.NewStateInformation;
var text = new
{
newStateInformation
};
return JsonConvert.SerializeObject(text);
}
[ActionName("AddNewCity")]
public object PostAddNewCity(RegionInformation newCityParam)
{
RegionOperations regionOperation = new RegionOperations(newCityParam.ParentID, newCityParam.CityName);
RegionInformation newStateInformation = regionOperation.NewStateInformation;
var text = new
{
newStateInformation
};
return JsonConvert.SerializeObject(text);
}
I have created an mvc application with SSO. I have added an Api controller, but whenever i try to get data from it, i get error.
URI= https://localhost:44305/api/graph/get
Error:
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /api/graph/get
Here is my Route config
public class RouteConfig
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
//routes.MapRoute(
// name: "Default",
// url: "{controller}/{action}/{id}",
// defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
routes.MapRoute(
name: "Default",
url: "api/{controller}/{action}/{id}",
defaults: new { controller = "Graph", action = "Get", id = UrlParameter.Optional }
);
}
}
This is my web api controller
public JsonResult<List<Claim>> Get()
{
var principal = (User as ClaimsPrincipal);
List<Claim> claimsList = new List<Claim>();
foreach (var claim in principal.Claims)
{
claimsList.Add(claim);
}
return Json<List<Claim>>(claimsList);
}
Not getting any build error. Any help is appreciated
Hy Guys.Thanks for all your help. I have figured out the solution. The answer is that in Global.asax.cs,
GlobalConfiguration.Configure(WebApiConfig.Register)
has to be called before
RouteConfig.RegisterRoutes(RouteTable.Routes).
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
}
}
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultDataApi",
routeTemplate: "Api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
FluentValidationModelValidatorProvider.Configure();
}
}
You should have 2 config files:
RoutesConfig.cs and WebApiConfig.cs
RoutesConfig should look like this:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
Log.Instance().Debug("Registering Routes");
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}
}
WebApiConfig.cs should look like this:
public static class WebApiConfig
{
public static void Configure(HttpConfiguration config)
{
ConfigureRouting(config);
ConfigureFormatters(config);
}
private static void ConfigureFormatters(HttpConfiguration config)
{
config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new NiceEnumConverter());
}
private static void ConfigureRouting(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional, #namespace = "api" });
}
}
Within global.asax.cs you should have something like this:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
// Register them in this order
GlobalConfiguration.Configure(WebApiConfig.Configure); //registers WebApi routes
RouteConfig.RegisterRoutes(RouteTable.Routes); //register MVC routes
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
Your controller does look a little strange why not let the default serialiser take care for things for you:
public IEnumerable<Claim> Get()
{
var principal = (User as ClaimsPrincipal);
List<Claim> claimsList = new List<Claim>();
foreach (var claim in principal.Claims)
{
claimsList.Add(claim);
}
return claimsList;
}
EDIT: removed redundant code
Assuming the name of your controller is GraphController, I think the url you should be using is /api/graph/get.
If it's named something different, you'll need to adjust the url accordingly.
Also, there's a separate .cs file in MVC projects you should use to manage WebAPI routes - WebApiConfig.cs
I'm making a call to http://localhost/AppTools.WebAPI/api/BulletinBoard/GetMessagesForApp/AppName, but it's returning a 404 error. I think this has to do with routing, but I'm not sure.
Here's the Web API method inside my BulletinBoard controller:
[HttpGet]
public HttpResponseMessage GetMessagesForApp(string id)
{
// get current, valid messages
var messages = (from i in db.BulletinBoards
where i.AppId == id &&
DateTime.Today >= i.DisplayFrom &&
DateTime.Today <= i.DisplayTo &&
i.IsActive == true
select new
{
Message = i.Message,
IntervalId = i.IntervalId,
Interval = i.Interval.IntervalDescription,
Timeout = i.Timout,
})
.ToList();
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, messages);
return response;
}
Here's my RouteConfig.cs:
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 }
);
}
The standard Get() and Get(int id) work fine, I didn't change the method name or signatures. Get() returns a complete list of records, Get(int id) returns a specific record. I want GetMessagesByApp(string id) to return a list of records specific to a certain AppName. Can you tell why this isn't working?
Here's my RouteConfig.cs:
The RouteConfig.cs file is used to define the routes for your ASP.NET MVC controllers. Those have absolutely nothing to do with the routes used by your Web API controllers. They are defined in the WebApiConfig.cs file.
So make sure you have declared your routes in the proper place:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "ApiWithActionName",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
Notice that I have added a custom route before the default one which will allow you to achieve the desired url pattern.
And then you could have the following controller action which will work fine:
// GET /api/controllername
// GET /api/controllername/get
[HttpGet]
public HttpResponseMessage Get()
{
...
}
// GET /api/controllername/get/123
[HttpGet]
public HttpResponseMessage Get(int id)
{
...
}
// GET /api/controllername/GetMessagesForApp/abc
[HttpGet]
public HttpResponseMessage GetMessagesForApp(string id)
{
...
}