3

I have a web api that runs in a WPF C# application. I have used Owin to implement it. If I send request by using /api prefix then it works as I expected.

http://localhost:8080/api/test/config?no=7

However, I need to remove the /api prefix. If I try the request below it does not work when I tried example code below.

http://localhost:8080/test/config?no=7

Is it possible to remove api word from requests?

Here is my code:

WebApp.Start<Startup>(url: "http://*:8080/");

    class Startup
    {
        Type ValuesControllerType = typeof(TestController);

        public void Configuration(IAppBuilder Builder)
        {
            var Instance = new HttpConfiguration();

            Instance.MapHttpAttributeRoutes();

            Instance.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "{controller}/{action}/{request}",
               defaults: new { request = RouteParameter.Optional }
           );

            Builder.UseWebApi(Instance);
        }
    }

    [RoutePrefix("test")]
    public class TestController : ApiController
    {    
        [HttpGet]
        [Route("config")]
        public string Config(string No)
        {
            try
            {
                return No;
            }
            catch (Exception e)
            {
                return string.Empty;
            }
        }
    }

I tried the answer in C# web api route for a webservice but did not work.

I get following error:

HTTP Error 503. The service is unavailable.
2
  • Could you show en error when [RoutePrefix("test")]? Commented Nov 30, 2017 at 15:44
  • I edited the question. I get Service Unavailable error. Commented Nov 30, 2017 at 15:56

2 Answers 2

2

On the Rest api open your WebApiConfig.cs you should find the following code:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}"
        );

Try removing api from there

Sign up to request clarification or add additional context in comments.

1 Comment

that's odd acording to this it should work. is the line config.MapHttpAttributeRoutes(); still enabled. and if you place a debug point in the webapiconfig.cs does it still go in there or do you only get into your owin class?
1

I faced the same problem in .Net Core 2 WebAPI project

here is my solution

[Produces("application/json")]
[Route("[controller]")]
public class DefaultController : Controller
{
    [Route("getUser")]

    public IActionResult GetUsers()
    {
         return Ok();
    }
}

the address is http://localhost:port/default/getuser

Comments

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.