Pergunta

Given the following simple Web API (not part of an MVC project, just an empty ASP.NET site) which I've taken from a tutorial:

Public Class AwesomeController
    Inherits ApiController

    Public Function [Get]() As String
        Return "Hello.  The time is " & DateTime.Now.ToString
    End Function

    Public Function Post(<FromBody> yourName As String) As String
        Return String.Format("Hi {0}, you posted here at {1}", yourName, DateTime.Now.ToString)
    End Function

End Class

I am creating the Route within global.asax like this:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    GlobalConfiguration.Configuration.Routes.Add("default", New HttpRoute("{controller}"))
End Sub

This works fine, but I've read that WebAPI Routing should preferably be created using MapHttpRoute so that the service can exist in isolation. Is this true?

If so, can anyone post the code to replace my global.asax version?

Update

I figured out the last bit, but would still appreciate knowing whether there is a 'correct' way?

RouteTable.Routes.MapHttpRoute("default", "{controller}")
Foi útil?

Solução

The MapHttpRoute extension method is simply a convenient way to register HttpRoute instances in your routing table. It's perfectly okay to manually create and register an HttpRoute without making use of the shorthand method, though.

If you want to understand the inner workings of MapHttpRoute, take a look at the source code: HttpRouteCollectionExtensions.cs.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top