0

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?

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

when I add the last line I get this: System.ArgumentException: 'A route named 'MS_attributerouteWebApi' is already in the route collection. Route names must be unique.
lol, forget about it, I already had it in the code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.