简体   繁体   English

如何从Web API项目的角度调用项目的MVC控制器动作?

[英]How to call MVC controller action of a project from view of web API project?

Project 1 is called DemoMVCProj.proj . 项目1称为DemoMVCProj.proj Its main files are index.html and DemoController . 它的主要文件是index.htmlDemoController

class DemoController : Controller {
// ----index action here----
    [HttpPost] public ActionResult CallAction()
    {
        // --some logic here--
        // --return action result--
    }
}

There is another project called DemoMVCWebApiProj.proj , where more than one web API is created. 还有另一个名为DemoMVCWebApiProj.proj项目,其中创建了多个Web API。

There is a third project called DemoUI.proj , which is an AngularJS project for creating views. 第三个项目是DemoUI.proj ,这是一个用于创建视图的AngularJS项目。

ApiController s are called under this project using DataFactory s. ApiController s的使用本项目叫DataFactory秒。

How can I call DemoController (from the DemoMVCProj project) from one of the view in the DemoUI project? 我怎么能叫DemoController (从DemoMVCProj项目)从视图的一个DemoUI项目?

Since DemoUI sounds like it's angular based, you would use $http to do any call's https://docs.angularjs.org/api/ng/service/ $http 由于DemoUI听起来像是基于角度的,因此您可以使用$ http进行任何呼叫的https://docs.angularjs.org/api/ng/service/ $ http

If your routing is normal, you would use the url of /controller/action 如果您的路由正常,则可以使用/ controller / action的网址

If you simply wanna go to a view in the MVC proj without ajax loading, simply write an tag with a href="/controller/action" 如果您只是想在不加载Ajax的情况下转到MVC项目中的视图,只需编写带有href =“ / controller / action”的标签

The main question is that why do you need DemoMVCProj.proj (project1) in the AngularJS application? 主要问题是,为什么在AngularJS应用程序中需要DemoMVCProj.proj(project1)?

AngularJS is great framework to bulid front-end client layer. AngularJS是构建前端客户端层的绝佳框架。 Angular services like $http and $resource well communicate with ApiControllers. 诸如$http$resource类的Angular服务可以与ApiControllers进行良好的通信。 They send data with Content-type: applications/json what is good supported by ApiControllers . 他们使用Content-type: applications/json发送数据, 而ApiControllers很好地支持了这些数据

You shouldn't use any Controllers which return ActionResults. 您不应使用任何返回ActionResults的Controllers Mishmashing Razor with AngularJS is bad and heavy. 用AngularJS混合Razor既糟又重。

The best solution is to use ASP.NET WebAPI2 to manage backend, and AngularJS for frontend. 最好的解决方案是使用ASP.NET WebAPI2来管理后端,并使用AngularJS来管理前端。 You doesn't need anything else. 您不需要任何其他东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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