MVC Web API controller

 

Create MVC WebAPI controller to post/get data

     
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    using System;
    using System.Net;
    using System.Threading.Tasks;
    using System.Web.Http;
    using System.Web.Http.Description;
    using System.Web.Http.Results;
    
    namespace Controllers
    {
        [RoutePrefix("root")]
        public class APIController : ApiController
        {
            public APIController()
            {
            }
    
            [HttpPost]
            [Route("data")]
            [ResponseType(typeof(string))]
            public async Task<IHttpActionResult> PostData(string request)
            {
                try
                {
                    if (!ModelState.IsValid)
                    {
                        //throw error
                    }
    
                    var data = "data";
    
                    return new NegotiatedContentResult<string>(
                        HttpStatusCode.Created,
                        data,
                        this);
                }
                catch (Exception e)
                {
                    throw;
                }
            }
    
            [HttpGet]
            [Route("data/events/{eventId}")]
            [ResponseType(typeof(string[]))]
            public async Task<IHttpActionResult> GetData([FromUri] string eventId)
            {
                try
                {
                    if (!ModelState.IsValid)
                    {
                        //throw error
                    }
    
                    var data = new string[] { "data" };
    
                    return new NegotiatedContentResult<string[]>(
                        HttpStatusCode.OK,
                        data,
                        this);
                }
                catch (Exception e)
                {
                    throw;
                }
            }
        }
    }

No comments:

Post a Comment

Move Github Sub Repository back to main repo

 -- delete .gitmodules git rm --cached MyProject/Core git commit -m 'Remove myproject_core submodule' rm -rf MyProject/Core git remo...