简体   繁体   English

路线不适用于 asp.net api 项目

[英]Routes not working on asp.net api project

I have a simple controller like this:我有一个简单的 controller 像这样:

using System.Web.Http;
using System.Configuration;

namespace LuloWebApi.Controllers
{
    [Authorize]
    [RoutePrefix("api/Version")]
    public class VersionController : ApiController
    {
        /// <summary>
        /// Gets the latest build version deployed
        /// </summary>
        /// <returns>string</returns>
        [HttpGet]
        public string Get()
        {
            return ConfigurationManager.AppSettings["buildversion"].ToString();
        }
    }

}

My startup is like this:我的启动是这样的:

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(LuloWebApi.App_Start.Startup))]
namespace LuloWebApi.App_Start
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app); 
            //
        }
    }
}

and my web api config和我的 web api 配置

using LuloWebApi.Components;
using System.Diagnostics.CodeAnalysis;
using System.Web.Http;
using System.Web.Http.ExceptionHandling;

namespace LuloWebApi
{
    [SuppressMessage("NDepend", "", Scope = "deep")]
    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}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Services.Add(typeof(IExceptionLogger), new AiExceptionLogger());
        }
    }
}

RouteConfig路由配置

 [SuppressMessage("NDepend", "", Scope = "deep")]
    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 }
            );
        }
    }

However when I go to: http://localhost:4478/api/Version但是,当我将 go 改为: http://localhost:4478/api/Version

I get resource can not be found, what am I missing here?我找不到资源,我在这里缺少什么?

You used "RoutePrefix" instead of "Route"您使用“RoutePrefix”而不是“Route”

[RoutePrefix("api/Version")]
public class VersionController : ApiController

[Route("api/Version")]
public class VersionController : ApiController

The route prefix will be added to your routes but is not a real route by itself.路由前缀将被添加到您的路由中,但它本身并不是真正的路由。

Edit: In your routing configuration you also need to specify you use the routing by attribute with that line:编辑:在您的路由配置中,您还需要指定您使用该行的属性路由:

config.MapHttpAttributeRoutes();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM