我說過這些路線和控制器:爲什麼這個Web Api路由不工作?
public class RouteConfig
{
public static readonly string MVC_ROUTING = "Default";
public static readonly string WEB_API_ROUTING = "Api";
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: MVC_ROUTING,
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
constraints: new { id = @"^[0-9]*$" }
);
routes.MapRoute(
name: WEB_API_ROUTING,
url: "api/{controller}/{action}/{id}",
defaults: new {id = UrlParameter.Optional },
constraints: new { id = @"^[0-9]*$" }
);
}
}
public class QuoteController : ApiController
{
private readonly QuoteService quoteService;
public QuoteController()
{
quoteService = new QuoteService();
}
// GET api/<controller>
public IEnumerable<QuoteModel> Get()
{
return quoteService.GetAllQuotes().Map<QuoteModel>();
}
// GET api/<controller>/5
public QuoteModel Get(int id)
{
return quoteService.GetQuoteById(id).Map<QuoteModel>();
}
// POST api/<controller>
public HttpResponseMessage Post(QuoteModel quoteModel)
{
Quote quote = quoteModel.Map<Quote>();
quoteService.AddNewQuote(quote);
string uri = Url.Link(RouteConfig.WEB_API_ROUTING, new { id = quote.QuoteId,controller="Quote",action="Get"});
return Request.CreateResponse(HttpStatusCode.OK, uri);
}
// PUT api/<controller>/5
public void Put(QuoteModel quote)
{
quoteService.Update(quote.Map<Quote>());
}
// DELETE api/<controller>/5
public void Delete(int id)
{
quoteService.DeleteQuote(id);
}
}
我不明白爲什麼這些路線工作:
但是這一次沒有:
非常感謝你,它完美的作品 –