WebApiConfig找不到URL问题

时间:2015-06-17 17:42:13

标签: c# angularjs http configuration asp.net-web-api

我在尝试做一个http.post时遇到了问题。我找不到404网址的错误。下面是我的WebApiConfig。我知道这与我的WebApiConfig的设置方式有关,但我不确定它有什么问题。

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web.Http;

 class WebApiConfig
 {
public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute("API Default", "api/{controller}/{action}/{id}",
        new { id = RouteParameter.Optional });

    //config.Routes.MapHttpRoute(
    //name: "API Default2",
    //routeTemplate: "api/{controller}/{action}/{id}",
    //defaults: new { id = RouteParameter.Optional });
}
}

ROUTE CONFIG

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Web;
 using System.Web.Mvc;
 using System.Web.Routing;

 namespace MvcApplication1
 {
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 }
        );
    }
}
 }

API CONTROLLER

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Net;
 using System.Net.Http;
 using System.Web.Http;

 namespace MvcApplication1.profile
 {
public class profile : ApiController
{
    // GET api/<controller>
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<controller>/5
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<controller>
    [ActionName("saveUser")]
    [HttpPost]
    public tblOwl saveUser(User newUser)
    {
        using (VSCDevEntities db = new VSCDevEntities())
        {

            tblOwl owl = new tblOwl
            {
                userName = newUser.username,
                Name = newUser.name,
                age = newUser.age,
                college = newUser.college,
                city = newUser.city
            };
            db.tblOwls.Add(owl);
            db.SaveChanges();



            return owl;
        }

    }

    // PUT api/<controller>/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/<controller>/5
    public void Delete(int id)
    {
    }



}

public class User
{
    public string username;
    public string name;
    public int age;
    public string college;
    public string city;
}
}

FACTORY

 (function () {
'use strict';

angular
    .module('app')
    .factory('profileFactory', profileFactory);

profileFactory.$inject = ['$http', '$q'];

function profileFactory($http, $q) {
    var urlBase = "/api/profile";

    var service = {
        saveUser: saveUser

    };

    return service;

    function saveUser(username, name, age, college, city) {
        var parm = {};
        parm.username = username;
        parm.name = name;
        parm.age = age;
        parm.college = college;
        parm.city = city;
        return $http.post(urlBase + "/saveUser", parm);
    }


}
 })();

0 个答案:

没有答案