Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a problem calling an ASP.NET API from my Index.cshtml file. I think the problem is in my Index file's action. My understanding is that the action is suppose to call the controller (I am new to this). When I click on the Choose file button I am sent to webpage that says "This localhost page can’t be found" Can anyone spot my problem? Thank you!

What I have tried:

My Index.cshtml file...

@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="container-fluid" ">

    <div class="panel panel-default" id="panel1">

        <form id="form1" 
              action="/api/Values" 
              method="post" 
              enctype="multipart/form-data">

            <label for="myfile">Select an file</label>
            <input id="btn_Upload" type="file" onchange="this.form.submit();" >

        </form>

    </div>

</div>


My ASP.NET API...

namespace WebApplication2.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpGet]
        public IEnumerable<string> Get()
        {
            return new string[] { "Values 1", "Values 2" };
        }

    }
}
Posted
Comments
j snooze 27-Jul-21 17:39pm    
I may be a little rusty on my MVC, but I believe you're controller methods name needs to match the name of the cshtml file(so change Get() to Index()), also if you are submitting a form, that would be an http post, not a get.
[no name] 30-Jul-21 21:44pm    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900