Luis Valencia
Luis Valencia

Reputation: 33988

Routes not working on asp.net api project

I have a simple controller like this:

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

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

I get resource can not be found, what am I missing here?

Upvotes: 0

Views: 133

Answers (1)

Arcord
Arcord

Reputation: 1909

You used "RoutePrefix" instead of "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();

Upvotes: 1

Related Questions