skip to Main Content

I’ve an WEB application with aspx page (No database, very simple web application). But Now I need to add some API services. I would add controler like when I programm in MVC but is it possible. I try but no success to call my controler, even directly in url

Imports Microsoft.AspNetCore.Mvc

<ApiController>
<Route("[Testouille]")>
Public Class LicencingYIController
   Inherits ControllerBase

<HttpGet>
Public Function GetCustomHeader() As ActionResult(Of String)
    Dim customHeader As String = Request.Headers("Custom-Header")
    If String.IsNullOrEmpty(customHeader) Then
        Return BadRequest("Custom header 'Custom-Header' is missing.")
    Else
        Return Ok("Received custom header: " & customHeader)
    End If
 End Function
End Class

I try from my visual studio https://localhost:myport/Testouille/GetCustomHeader
but nothing pass. I don’t enter my GetCustomHeader function (break point)

Then I just ask if is it possible or perhaps finnally better to make an asmx ?

Thanks fot your help.

2

Answers


  1. Chosen as BEST ANSWER

    Finnally ... U can with web api controler

    Imports System.Net
    Imports System.Web.Http
    
    Public Class TestouilleController
    Inherits ApiController
    
    <HttpGet>
    Public Function GetCustomHeader() As IHttpActionResult
        Dim customHeader As String = Request.Headers.GetValues("Custom-Header").FirstOrDefault()
    
        If String.IsNullOrEmpty(customHeader) Then
            Return Content(Net.HttpStatusCode.BadRequest, "Custom header 'Custom-Header' is missing.")
        Else
            Return Ok("Received custom header: " & customHeader)
        End If
    End Function
    
    End Class
    

    I let the header reader... always good to take Don't forget initialyse the route

    Public Class WebApiConfig
    Public Shared Sub Register(config As HttpConfiguration)
    
        config.MapHttpAttributeRoutes()
    
        config.Routes.MapHttpRoute(
            name:="DefaultApi",
            routeTemplate:="API/{controller}/{id}",
            defaults:=New With {.id = RouteParameter.Optional}
        )
    End Sub
    End Class
    

    To call it in global.asax.start

    and Call API, like that

    <WebMethod(EnableSession:=True)>
     Public Sub GetTesteur()
        Try
            Dim strbrut As String = ""
            Using WC As New WebClient
                WC.Headers.Add("Custom-Header", "Toto")
                strbrut = WC.DownloadString("https://localhost:44367/api/Testouille/GetCustomHeader") 
            End Using
        Catch ex As Exception
        End Try
    End Sub
    

  2. No, it’s more easy create new.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search