AngularJS控制器找不到C#控制器
我希望你们能帮助我 我有一个简单的项目仍处于初级阶段,但无法让AngularJS控制器(IngreditAdmin.Controller)找到我的C#API控制器 我收到一个错误:404/未找到:请求URL: 我的AngularJS控制器和C#控制器在不同的项目中,我在AngularJS项目中引用了我的C#控制器项目 我也尝试过各种拼写等,但我想我可能遗漏了一些东西 这是我的AngularJS控制器:IngRadientAdminController:AngularJS控制器找不到C#控制器,c#,angularjs,asp.net-web-api,C#,Angularjs,Asp.net Web Api,我希望你们能帮助我 我有一个简单的项目仍处于初级阶段,但无法让AngularJS控制器(IngreditAdmin.Controller)找到我的C#API控制器 我收到一个错误:404/未找到:请求URL: 我的AngularJS控制器和C#控制器在不同的项目中,我在AngularJS项目中引用了我的C#控制器项目 我也尝试过各种拼写等,但我想我可能遗漏了一些东西 这是我的AngularJS控制器:IngRadientAdminController: (function () { "u
(function () {
"use strict";
var controllerId = 'admin';
angular
.module('app.ingredientAdmin')
.controller('ingredientAdminController', ingredientAdminController, controllerId);
ingredientAdminController.$inject = ['$http', '$scope', 'common'];
function ingredientAdminController($http, $scope, common) {
var vm = $scope;
vm.formSubmmision = true;
vm.title = 'Ingredients Admin';
vm.ingredientData = null;
vm.save = save;
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
activate();
function activate() {
common.activateController([], controllerId)
.then(function () { log('Activated Admin View'); });
}
// Save
function save() {
if (vm.formIngredientsAdmin.$valid) {
postNewData();
}
else {
logger.error('Error: Validation failed. Please correct data and try again');
vm.formSubmmision = false;
}
}
function postNewData() {
//prepare data
var data = {
IngredientId: 0,
IngredientName: vm.NewIngredient.IngredientName,
IngredientDescription: vm.NewIngredient.IngredientDescription
}
$http.post('/api/ingredientAdmin/PostIngredient/', data)
.then(postDataComplete)
.catch(getDataFailed);
function postDataComplete(response) {
vm.NewIngredient = null;
vm.formSubmmision = true;
vm.formIngredientsAdmin.$setPristine();
vm.formIngredientsAdmin.$setUntouched();
return vm.NewIngredient;
}
function getDataFailed(error) {
log('Failed to create new Ingredient ' + error.data.Message);
return;
}
}
};
}
)();
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RecipeManager.Models;
using RecipeManager.DTO;
using RecipeManger.Common;
namespace RecipeManager.Controllers.Api
{
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
//[Authorize(Roles = "Admin, SupportCallIndex")]
public HttpResponseMessage UpdateIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.UpdateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
[HttpPost]
//[Authorize(Roles = "Admin, SupportCallIndex")]
public HttpResponseMessage DeleteIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.DeleteIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
public HttpResponseMessage GetAll()
{
try
{
CommandResult<Ingredient> result = dto.ReadIngredient();
return Request.CreateResponse((result.Status == CommandStatus.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError), result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
}
}
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
namespace RecipeManager.UI.Htmll5
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
这是我的C#controller:InCredentAdminController:
(function () {
"use strict";
var controllerId = 'admin';
angular
.module('app.ingredientAdmin')
.controller('ingredientAdminController', ingredientAdminController, controllerId);
ingredientAdminController.$inject = ['$http', '$scope', 'common'];
function ingredientAdminController($http, $scope, common) {
var vm = $scope;
vm.formSubmmision = true;
vm.title = 'Ingredients Admin';
vm.ingredientData = null;
vm.save = save;
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
activate();
function activate() {
common.activateController([], controllerId)
.then(function () { log('Activated Admin View'); });
}
// Save
function save() {
if (vm.formIngredientsAdmin.$valid) {
postNewData();
}
else {
logger.error('Error: Validation failed. Please correct data and try again');
vm.formSubmmision = false;
}
}
function postNewData() {
//prepare data
var data = {
IngredientId: 0,
IngredientName: vm.NewIngredient.IngredientName,
IngredientDescription: vm.NewIngredient.IngredientDescription
}
$http.post('/api/ingredientAdmin/PostIngredient/', data)
.then(postDataComplete)
.catch(getDataFailed);
function postDataComplete(response) {
vm.NewIngredient = null;
vm.formSubmmision = true;
vm.formIngredientsAdmin.$setPristine();
vm.formIngredientsAdmin.$setUntouched();
return vm.NewIngredient;
}
function getDataFailed(error) {
log('Failed to create new Ingredient ' + error.data.Message);
return;
}
}
};
}
)();
using System.Net;
using System.Net.Http;
using System.Web.Http;
using RecipeManager.Models;
using RecipeManager.DTO;
using RecipeManger.Common;
namespace RecipeManager.Controllers.Api
{
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
//[Authorize(Roles = "Admin, SupportCallIndex")]
public HttpResponseMessage UpdateIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.UpdateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
[HttpPost]
//[Authorize(Roles = "Admin, SupportCallIndex")]
public HttpResponseMessage DeleteIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.DeleteIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
public HttpResponseMessage GetAll()
{
try
{
CommandResult<Ingredient> result = dto.ReadIngredient();
return Request.CreateResponse((result.Status == CommandStatus.Success ? HttpStatusCode.OK : HttpStatusCode.InternalServerError), result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
}
}
using System.Web.Http;
using Microsoft.Owin.Security.OAuth;
namespace RecipeManager.UI.Htmll5
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
这是我的应用程序框架的结构:
非常感谢 如果要保持代码的当前状态,请在默认路由之前添加此路由:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
如@Lex所述,为
id
param添加了默认值。如果您想保持代码的当前状态,请在默认值之前添加此路由:
config.Routes.MapHttpRoute(
name: "ActionApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
正如@Lex所提到的,为
id
param添加了默认值。WebApi/HttpPost对此有一些奇怪的警告
请参见我的答案(不是问题,而是我的答案):
注意Api控制器上的方法:
[System.Web.Http.HttpPost]
public MyOutputObject DoSomething([FromBody]MyInputObject args)
{
Console.Writeline(args.ToString());
}
[RoutePrefix("api")]
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[Route("PostIngredient")]
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
请注意这篇文章:
这篇文章呢
WebApi/HttpPost对此有一些奇怪的警告 请参见我的答案(不是问题,而是我的答案): 注意Api控制器上的方法:
[System.Web.Http.HttpPost]
public MyOutputObject DoSomething([FromBody]MyInputObject args)
{
Console.Writeline(args.ToString());
}
[RoutePrefix("api")]
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[Route("PostIngredient")]
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
请注意这篇文章:
这篇文章呢
您可以将RoutePrefix和Route添加到控制器中:
[System.Web.Http.HttpPost]
public MyOutputObject DoSomething([FromBody]MyInputObject args)
{
Console.Writeline(args.ToString());
}
[RoutePrefix("api")]
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[Route("PostIngredient")]
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
希望有帮助。您可以将RoutePrefix和Route添加到控制器中:
[System.Web.Http.HttpPost]
public MyOutputObject DoSomething([FromBody]MyInputObject args)
{
Console.Writeline(args.ToString());
}
[RoutePrefix("api")]
public class IngredientAdminController : ApiController
{
private RecipeManager.DTO.IngredientAdminDTO dto = new IngredientAdminDTO();
[Route("PostIngredient")]
[HttpPost]
public HttpResponseMessage PostIngredient(Ingredient ingredient)
{
try
{
CommandResult<Ingredient> result = dto.CreateIngredient(ingredient);
return Request.CreateResponse(HttpStatusCode.OK, result);
}
catch (RecipeManagerException ex)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message);
}
}
希望能有所帮助。您需要发布到
/api/ingredientAdmin
,而不是/api/ingredientAdmin/PostIngredient/
或添加新路由:api/{controller}/{action}/{id}
您需要发布到/api/ingredientAdmin
,而不是/api/ingredientAdmin/PostIngredient/
或添加新路由:api/{controller}/{action}/{id}
谢谢你的建议。我确信这是我的问题(其中之一),但我就是无法让它工作。仍然会收到相同的错误。我确实更改了routeTemplate以包含{action}。还有什么我可能遗漏的吗?在调试时,它直接从我的$http.post请求跳到我的错误捕获。您是否尝试调试api控制器只是为了检查它的命中率?@onmyway您可能需要标记{id}
使用defaults
属性作为可选。感谢您的建议。我确信这是我的问题(其中之一),但我无法让它正常工作。仍然会出现相同的错误。我确实更改了routeTemplate以包含{action}。还有什么我可能遗漏的吗?在调试时,它直接从我的$http.post请求跳到我的错误捕获。您是否尝试调试api控制器只是为了检查它的命中率?@onmyway您可能需要标记{id}
使用defaults
属性作为可选。感谢您的反馈。不幸的是,我仍然坚持这一点。感谢您的反馈。不幸的是,我仍然坚持这一点。感谢您的帮助。我喜欢您的方法。不幸的是,我仍然没有找到404:文件。我已经决定深入挖掘一下,并且正在忙于编写关于W的一些教程ebApi从前到后。解决问题后我将恢复。请尝试本地主机:3021/api/PostIngredient地址,不带“ingridientAdmin”,如果没有,希望它有帮助-让我知道。谢谢你的帮助。我喜欢你的方法。不幸的是,我仍然得到404:not found。我决定再深入一点,现在正忙着从前面到后面的一些WebApi教程。解决问题后我将恢复。请尝试localhost:3021/api/postingCredit地址,而不使用“ingridientAdmin”,希望能有所帮助,如果没有,请告诉我。