locked
Have 2 Get() Methods In API RRS feed

  • Question

  • User974100899 posted

    Hi - I am sure this is super simple to an experienced asp.net coder, but I want to have 3 GET() methods. However I am getting this error

    already defines a member called 'Get' with the same parameter types

    This is my code - what should I change so this will compile as I wish

            public IEnumerable<appinfo> Get()
            {
                using (XamarinEntities entities = new XamarinEntities())
                    return entities.appinfoes.ToList();
            }
            public IEnumerable<string> Get()
            {
                return new string[] { "id", "fname", "lname", "phone", "company", "approveduser" };
            }
            public appinfo Get(string email)
            {
                using (XamarinEntities entities = new XamarinEntities())
                    return entities.appinfoes.FirstOrDefault(e => e.email == email);
            }

    Wednesday, November 28, 2018 2:51 AM

Answers

  • User1120430333 posted

    One way to do it is to use the ActionName attribute. This is a VB.NET example.

    https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/routing-in-aspnet-web-api

    Imports System.Web.Mvc
    Imports System.Web.Routing
    
    Public Module RouteConfig
        Public Sub RegisterRoutes(ByVal routes As RouteCollection)
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}")
    
            routes.MapRoute(
                name:="Default",
                url:="api/{controller}/{action}/{id}",
                defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
            )
        End Sub
    End Module
    Imports System.Web.Http
    Imports DAL
    Imports Entities
    
    Namespace Controllers
    
        <CustomExceptionFilter>
        Public Class TaskController
            Inherits ApiController
    
            Private ReadOnly _daotask As IDaoTask
    
            public sub New (daotask As IDaoTask)
                _daotask = daotask
            End sub
    
            <HttpGet>
            <ActionName("GetTaskById")>
            public Function  GetTaskById(ByVal id As int32) as DtoTask
                return _daotask.GetTaskById(id)
            End Function
            
            <HttpGet>
            <ActionName("GetTasksByProjectId")>
            public  Function GetTasksByProjectId(byval id as int32) as List(Of DtoTask)
                return _daotask.GetTasksByProjectId(id)
            End Function
    
            <HttpPost>
            <ActionName("CreateTask")>
            public sub  CreateTask(ByVal dto As DtoTask)
                Call _daotask.CreateTask(dto)
            End sub
           
            <HttpPost>
            <ActionName("UpdateTask")>
            public sub  UpdateTask(ByVal dto As DtoTask)
                Call _daotask.UpdateTask(dto)
            End sub
            
            <HttpPost>
            <ActionName("DeleteTask")>
            public sub  DeleteTask(byval dto As DtoId)
                Call _daotask.DeleteTask(dto.Id)
            End sub
            
        End Class
    End Namespace
    public function GetTasksByProjIdApi(id as int32) as List(Of DtoTask) Implements IWebApi.GetTasksByProjIdApi
    
                Dim dtotasks = new List(Of DtoTask)
    
                dim url = "http://localhost/WebApiVB/api/task/GetTasksByProjectId?id=" & id
    
                Using webclient As New WebClient
                    dim json  = webclient.DownloadString(url)
                    Dim tasks = JsonConvert.DeserializeObject(of List(Of DtoTask))(json)
                    dtotasks = tasks
                End Using
    
                Return dtotasks
    
            End function
            
            public Function  GetTaskByIdApi(id As int32) As DtoTask Implements IWebApi.GetTaskByIdApi
    
                dim dto as DtoTask
    
                dim url = "http://localhost/WebApiVB/api/task/GetTaskById?id=" & id
    
                Using webclient As New WebClient
                    dim json  = webclient.DownloadString(url)
                    Dim task = JsonConvert.DeserializeObject(of DtoTask)(json)
                    dto = task
                End Using
    
                return dto
    
            End Function
    
            public sub CreateTaskApi(dto As DtoTask) Implements IWebApi.CreateTaskApi
    
                Dim reqString As byte()
    
                Using webclient As New WebClient
    
                    dim url as string = "http://localhost/WebApiVB/api/task/CreateTask"
                    webClient.Headers("content-type") = "application/json"
                    reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dto, Formatting.Indented))
                    webClient.UploadData(url, "post", reqString)
    
                End Using
              
            End sub
            
            public sub UpdateTaskApi(dto As DtoTask) Implements IWebApi.UpdateTaskApi
    
                Dim reqString As byte()
    
                Using webclient As New WebClient
    
                    dim url as string = "http://localhost/WebApiVB/api/task/UpdateTask"
                    webClient.Headers("content-type") = "application/json"
                    reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dto, Formatting.Indented))
                    webClient.UploadData(url, "post", reqString)
    
                End Using
                
            End sub
            
            public sub DeleteTaskApi(dto As DtoId) Implements IWebApi.DeleteTaskApi
    
                Dim reqString As byte()
    
                Using webclient As New WebClient
    
                    dim url as string = "http://localhost/WebApiVB/api/task/DeleteTask"
                    webClient.Headers("content-type") = "application/json"
                    reqString = Encoding.Default.GetBytes(JsonConvert.SerializeObject(dto, Formatting.Indented))
                    webClient.UploadData(url, "post", reqString)
    
                End Using
    
            End sub
    end class

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, November 28, 2018 7:47 AM