简体   繁体   中英

How to call ASP.NET Core Web MVC from Web API

I have ASP.NET Core Web MVC application that is taking data from ASP.NET Core Web API. But there is one situation when I need to call MVC from API, when data is updated on API side.

My code in MVC Controller is

public class HomeController : Controller
{
    [HttpGet,Route("updateOverview")]
    public void GetOverview()
    {
        // some code
    }
}

and on API side I have this

             try
            {
                UriBuilder uri = new UriBuilder("https://localhost:44324/Home/updateOverview");     // mvc url
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri.Uri);
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            }
            catch(Exception e)
            { }

but response code is 404. Then I try this

            HttpResponseMessage responseMessage1 = await _apiClient.GetAsync("https://localhost:44324/Home/updateOverview");

but also get 404.

I also tried creating API controller on MVC side so instead of trying to hit this controller

public class HomeController : Controller
{}

I try hitting this

[Route("api/[controller]")]
[ApiController]
public class GetDataController : ControllerBase
{}

but also getting 404.

try = >>

[HttpGet,Route("home/updateOverview")]

or

[HttpGet,Route("[controller]/updateOverview")] 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM