子目录应用程序中的Web API [英] web api in subdirectory application
问题描述
我正在创建一个带有指向各种应用程序链接的中心站点,这些链接都存储在主中心站点下的子目录中.这些应用程序之一具有Web API,在localhost上调试时可以正常工作.但是,当我发布到子目录时,尝试打api时出现404错误.基于其他问题,我确保在IIS中为子目录创建应用程序".这是我第一次尝试使用Web api,我不知道还要尝试什么,甚至不知道应该提供什么其他信息来澄清问题.
I am creating a hub site with links to various apps, which are all stored in subdirectories under the main hub site. One of these apps has a web api, which works fine when debugging on localhost. However, when I publish to the subdirectory, I get a 404 error when trying to hit the api. Based on other questions, I made sure to "create application" in IIS for the subdirectory. This is my first web api attempt and I don't know what else to try, or even what other info I should give to clarify the problem.
中心网站:apps.somesite.com
应用程序站点:apps.somesite.com/someapp/
hub site: apps.somesite.com
app site: apps.somesite.com/someapp/
工作api :本地主机:12345/api/tasks
working api: localhost:12345/api/tasks
404无法正常使用的api,应用会自动尝试使用此:apps.somesite.com/api/tasks
404无法正常工作的api,我在中输入以下内容:apps.somesite.com/someapp/api/tasks(
404 not working api, app automatically tries to use this one: apps.somesite.com/api/tasks
404 not working api, I type this one in: apps.somesite.com/someapp/api/tasks (
这是我的控制器:
namespace TaskTracker.Controllers
{
[RoutePrefix("api")]
public class TasksController : ApiController
{
ModelEntities taskTrackerApi = new ModelEntities();
// Returns a list of all tasks
[Route("tasks")]
[HttpGet]
public IEnumerable<taskTracker_getTasks_Result> getTasks()
{
return taskTrackerApi.taskTracker_getTasks().AsEnumerable();
}
// To create a new task
[Route("add/{arkona}")]
[HttpPost]
public int addTask(string arkona)
{
return taskTrackerApi.taskTracker_addTask(arkona);
}
// To update a task
[Route("update")]
[HttpPut]
public int updateTask([FromBody]TaskTracker.Models.Task task)
{
int id = task.Id;
string taskName = task.TaskName;
string link = task.Link;
DateTime lastUpdate = task.LastUpdate;
string lastUpdatedBy = task.LastUpdatedBy;
DateTime dueDate = task.DueDate;
string notes = task.Notes;
return taskTrackerApi.taskTracker_updateTask(id, taskName, link, lastUpdate, lastUpdatedBy, dueDate, notes);
}
// To delete a task
[Route("delete/{id:int}")]
[HttpDelete]
public int deleteTask(int id)
{
return taskTrackerApi.taskTracker_deleteTask(id);
}
// Returns a list of the dealerships
[Route("dealers")]
[HttpGet]
public IEnumerable<taskTracker_getDealers_Result> getDealers()
{
return taskTrackerApi.taskTracker_getDealers().AsEnumerable();
}
}
}
这是我的api配置:
namespace Task_Tracker
{
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 }
);
// Use camelcase for JSON data
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
}
}
最后,这是我为调用api提供的服务:
Finally, here is my service in angular to call the api:
app.service('taskService', ['$http', function ($http) {
this.getTasks = function () {
return $http.get('/api/tasks');
};
this.createTask = function (arkona) {
return $http.post('/api/add/' + arkona);
};
this.updateTask = function (task) {
return $http.put('/api/update', task);
};
this.deleteTask = function (id) {
return $http.delete('/api/delete/' + id);
};
this.getDealers = function () {
return $http.get('/api/dealers');
};
}]);
推荐答案
最终有两个问题.第一,该应用程序正在发布到过旧版本的IIS;还有两个,我需要删除我的angularjs http get调用上的前导斜杠.
There ended up being two problems. One, the app was being published to a too-old version of IIS; and two, I needed to remove the leading forward slash on my angularjs http get call.
错误:
return $http.get('/api/tasks');
正确:
return $http.get('api/tasks');
这篇关于子目录应用程序中的Web API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!