bbusdriver
bbusdriver

Reputation: 1627

Asp.net core passing multiple parameter + web api

I am submitting a form that passes three values to the controller, which are email, fullname and id fields.

 @using (Html.BeginForm("SubmitResult", "TestAPI", FormMethod.Post, new { id = "postEmailForm" }))    
{
    <div id="details-container">
        <input type="text" name="email" />
        <input type="text" name="fullName" />
        <input type="text" name="studentId" />

        <button type="submit" id="send">Send</button>
    </div>
}   

Controller:

  [HttpPost("SubmitResult/{email}/{fullName}/{studentId}")]
    [Authorize(Roles = "Admin, Shop")]
    public IActionResult SubmitResult(string email, string fullName, long? studentId)
    {
    }

However, when I click on submit button, it throws an error message in the console.

OPTIONS https://localhost:50138/TestAPI/SubmitResult net::ERR_SSL_PROTOCOL_ERROR.

Headers:

Request URL: https://localhost:50138/TestAPI/SubmitResult

Referrer Policy: no-referrer-when-downgrade

How do I properly decorate the attribute in the controller, so I can pass multiple parameters to test API using Postman?

I was expecting something like below to work for testing.

http://localhost:50138/api/TestAPI/SubmitResult/[email protected]/MikeShawn/2

Upvotes: 1

Views: 1039

Answers (1)

Alexey Andrushkevich
Alexey Andrushkevich

Reputation: 6162

There are few issues with your code.

First issue is that it looks like when you post the data it tries to send it using a cross-origin request. If this is on purpose then you have to add CORS middleware in your pipe. If not - you have to figure out why it happens and fix it. There are not enough details in your question to say why it happens.

Two URLs have the same origin if they have identical schemes, hosts, and ports.

Second issue is that you are trying to send data by adding parameters to URL. This is wrong because the data will be sent in the request body. So regarding HttpPost attribute it should look like this:

[HttpPost]
[Authorize(Roles = "Admin, Shop")]
public IActionResult SubmitResult(string email, string fullName, long? studentId)
{
}

UPDATE

Just looked at your question again. It seems the page with form itself was opened using http scheme, but the POST request is actually going to https scheme. So to resolve first issue make sure that page with form is loaded using https scheme too.

Upvotes: 1

Related Questions