web api in subdirectory application

旧街凉风 提交于 2021-01-29 07:56:16

问题


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.

hub site: apps.somesite.com
app site: apps.somesite.com/someapp/

working api: localhost:12345/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 (

Here's my controller:

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();
        }

    }
}

Here's my api config:

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();
        }
    }
}

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');
    };

}]);

回答1:


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.

Incorrect:

return $http.get('/api/tasks');

Correct:

return $http.get('api/tasks');


来源:https://stackoverflow.com/questions/36872458/web-api-in-subdirectory-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!