locked
Web.Http.Results RRS feed

  • Question

  • User-1104215994 posted

    Hi there,

    In my web API, I am using a filter. On authorization, I am checking a valid time interval and if not inside this interval I am giving a response as follows. I wonder if I can return web.http.results instead of httpresponsemessage?

    var response = new HttpResponseMessage
                    {
                        StatusCode = (HttpStatusCode)429,
                        ReasonPhrase = "Invalid Request Time",
                        Content = new StringContent("Requests are permitted between " + WebConfigurationManager.AppSettings["TimeStart"].ToString() + " AM and " + WebConfigurationManager.AppSettings["TimeEnd"].ToString() + " PM.")
    
                    };

    The response is like this;

    {
        "Message": "Requests are permitted between 11:00 AM and 22:30 PM."
    }

    Best Regards.

    Monday, April 13, 2020 5:55 PM

Answers

  • User-1104215994 posted

    This solved my problem,

    Content = new StringContent("{\"Message\":\"Requests are permitted between " +
                                                    WebConfigurationManager.AppSettings["TimeStart"].ToString() +
                                                    " AM and " + WebConfigurationManager.AppSettings["TimeEnd"].ToString() +
                                                    " PM.\"}", System.Text.Encoding.UTF8, "application/json"),

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2020 12:08 PM

All replies

  • User-17257777 posted

    Hi cenk1536,

    The response is like this;

    {
        "Message": "Requests are permitted between 11:00 AM and 22:30 PM."
    }

    The result type is json, you can just return like below:

    public IHttpActionResult Get()
            {
                return Json(new { Message = "Requests are permitted between 10 AM and 10 PM." });
            }

    Best Regards,

    Jiadong Meng

    Tuesday, April 14, 2020 10:08 AM
  • User-1104215994 posted

    I mean here;

    public override void OnAuthorization(System.Web.Http.Controllers.HttpActionContext actionContext)
            {
                //Check Request Start-End Period
                if (!CheckRequestTime())
                {
                    var response = new HttpResponseMessage
                    {
                        StatusCode = (HttpStatusCode) 429,
                        ReasonPhrase = "Invalid Request Time",
                        Content = new StringContent("Requests are permitted between " +
                                                    WebConfigurationManager.AppSettings["TimeStart"].ToString() +
                                                    " AM and " + WebConfigurationManager.AppSettings["TimeEnd"].ToString() +
                                                    " PM."),
    
                        
                       
    
                    };
    
                    actionContext.Response = response;
    
    
                }
            }

    Tuesday, April 14, 2020 11:26 AM
  • User-1104215994 posted

    This solved my problem,

    Content = new StringContent("{\"Message\":\"Requests are permitted between " +
                                                    WebConfigurationManager.AppSettings["TimeStart"].ToString() +
                                                    " AM and " + WebConfigurationManager.AppSettings["TimeEnd"].ToString() +
                                                    " PM.\"}", System.Text.Encoding.UTF8, "application/json"),

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, April 14, 2020 12:08 PM