dimanche 2 juillet 2017

Redirect to "GET" Web Page After HTML Form "POST"

Using ASP .NET Web API I am creating a RESTful API which processes all of your typical HTTP actions: GET, POST, PUT, DELETE

I have the following code which processes my POST action within my "CarAdvertisementController" class.

        [HttpPost]
        [Route("api/caradvertisement")]
        public HttpResponseMessage PostCarAdvertisement([FromBody] HttpCarWrapper CarWrap)
        {
            HttpResponseMessage Response = null;

        List<CarAdvertisement> RegisteredVehicles = new List<CarAdvertisement>();

        CarAdvertisement MyCar = null;

        RegisteredVehicles = CarAdvertisementController.RegisterVehicle(CarWrap, out MyCar);

        if (RegisteredVehicles != null)
        {
            CarAdvertisementController.CarRepository = new CarRepository(ref RegisteredVehicles);
            // Create a new response.
            Response = Request.CreateResponse<CarAdvertisement>(HttpStatusCode.Created, MyCar);

            StringBuilder RegisteredCarsBuilder = new StringBuilder();

            //Response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

            StringBuilder JsonStringBuilder = new StringBuilder();
            string jsonString = String.Empty;

            foreach(CarAdvertisement RegisteredCar in RegisteredVehicles)
            {
                // Add the CarAdvertisement object as part of a list of JsonStrings.
                JsonStringBuilder.Append(JsonConvert.SerializeObject(
                RegisteredCar, Formatting.Indented,
                new JsonConverter[] { new StringEnumConverter() }));
            }

            if (JsonStringBuilder != null)
            {
                jsonString = JsonStringBuilder.ToString();
            }

            Response.Content = new StringContent(jsonString);

            //Response.Headers.Location =
                //new Uri(Url.Link("DefaultApi", new { controller = "caradvertisements", id = 1 }));
            Response = Request.CreateResponse(HttpStatusCode.Moved);
            Response.Headers.Location = new Uri("http://www.yahoo.com");   
        }

        return Response;
    }

And here is my HTML/JavaScript for the web/frontend portion of this action.

<div id="body">
    <ul id="caradvertisement"></ul>
</div>

<form id="saveCarAdvertisementForm" method="POST">
    <h3>Create A New Car Advertisement</h3>
    <p>
        <label for="New">New or Used:</label>
        <div align="left">
            <select name="mydropdown">
                <option value="New">New</option>
                <option value="Used">Used</option>
            </select>
        </div>
    </p>
    <p>
        <label for="Id">Car Id:</label>
        <input type="text" name="Id" />
    </p>
    <p>
        <label for="Title">Car Title:</label>
        <input type="text" name="Title" />
    </p>
    <p>
         <div align="left">
            <label for="Fuel">Car Fuel Type:</label>
            <select name="mydropdown">
                <option value="Gasoline">Gasoline</option>
                <option value="Diesel">Diesel</option>
            </select>
        </div>
    </p>
    <p>
        <label for="Price">Car Price:</label>
        <input type="text" name="Price" />
    </p>
    <p>
        <label for="Mileage (Used Car)">Mileage:</label>
        <input type="text" name="Mileage" />
    </p>
    <p>
        <label for="First Registration">First Registration:</label>
        <input type="text" name="FirstRegistration" />
    </p>
    <input type="button" id="saveCarAdvertisement" value="Save" />
</form>

@section scripts{
<script type="text/javascript">
$(function()
{
    $.getJSON('/api/caradvertisements', function(caradvertisementsJsonPayload)
    {
        $(caradvertisementsJsonPayload).each(function (i, item)
        {
            $('#caradvertisement').append('<li>' + item.Name + '</li>');
        });
    });
});
$('#saveCarAdvertisement').click(function ()
{
        $.post("api/caradvertisement",
          $("#saveCarAdvertisementForm").serialize(),
          function (value)
          {
              $('#caradvertisement').append('<li>' + value.Name + '</li>');
          },
          "json"
    );
});
</script>
}

It creates an HTML form and I want the user to be able to click the "Send" button and have their browser be "Redirected" to the URL http://localhost:6058/api/caradvertisement/1 for example. Right now as you can see, I am just trying to even test this with "www.yahoo.com" for example.

The end result is I want this URL to display the data of the "CarAdvertisement" Object using my "GET" action processing within the same CarAdvertisementController class.

At the moment, when I run this code and they press "Send" it doesn't actually send them to the new URL. And when I type in that URL into my browser the browser response says that it "does not support GET"

Can anyone let me know what I'm doing wrong here?

Thanks in advance!




Aucun commentaire:

Enregistrer un commentaire