Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 是否可以使用ApiController调用在控制器中编写的操作?_Asp.net Mvc_Visual Studio_Asp.net Mvc Controller - Fatal编程技术网

Asp.net mvc 是否可以使用ApiController调用在控制器中编写的操作?

Asp.net mvc 是否可以使用ApiController调用在控制器中编写的操作?,asp.net-mvc,visual-studio,asp.net-mvc-controller,Asp.net Mvc,Visual Studio,Asp.net Mvc Controller,我已经在控制器中编写了一些操作,但我需要使用它们,因为它们是API。 所以我想知道是否有可能在ApicController中调用单个控制器操作 最后,我希望有这样的东西: public MyController : Controller { public ActionResult Index() { return View(); } //or any more complex action } public MyApis : ApiContro

我已经在控制器中编写了一些操作,但我需要使用它们,因为它们是API。
所以我想知道是否有可能在ApicController中调用单个控制器操作

最后,我希望有这样的东西:

public MyController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    //or any more complex action
}


public MyApis : ApiController
{
    // some code that returns the same
    // MyController/Index/View result as an API URI     
}
选择此选项的主要原因是我正在使用VisualStudio开发多层解决方案。

我在一个专用项目中有控制器,而我想在另一个项目中有APIController

如果我理解正确的话,你有几个选择

我建议您首先创建一个包含所有api控制器的单独文件夹。从这里,您可以从ActionResult方法中提取流程,并相应地进行调用

或者您可以将控制器类型从ActionResult更改为JsonResult

public JsonResult DoStuff()
{
   // some code that returns the same
   return Json(new { Data = model } , JsonRequestBehavior = JsonRequestBehavior.AllowGet);
}

希望这有帮助

当然!因此,对于与要渲染的视图相关的操作方法,可以编写如下基本内容

没有进程的控制器

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
    //or some model initiation if needed
    public ActionResult Index(ViewModel model)
    {
        return View(model);
    }
}
从这里开始,您可以像创建ActionResult方法一样创建API控制器方法。唯一的区别是返回类型。您的api控制器应该对应于您打算对其运行查询的任何模型实例。就我个人而言,我更喜欢创建一个与每个数据库抽象相关的模型,如ContactController、AccountController等

    private readonly IRepository repo;

    //This is a generic repository I do not know if you are using this methodology.
    //but this is strictly for demo purposes.
    public ValuesController(IRepository<SomeModel> repo)
    {
        this.repo = repo;
    }
    public IEnumerable<SomeModel> Get()
    {
        var commands = repo.GetAll();
        return commands.ToList();
    }

    // GET api/values/5
    public IQueryable<SomeModel> Get(int id)
    {
        var commands = repo.GetAll().AsQueryable();
        return commands;
    }

    // POST api/values
    public HttpResponseMessage Post(SomeModel model)
    {
        repo.Add(model);

        var response = Request.CreateResponse<SomeModel>(HttpStatusCode.Created, model);

        string uri = Url.Link("DefaultApi", new { id = model.Id});

        response.Headers.Location = new Uri(uri);

        return response;
    }

    // PUT api/values/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE api/values/5
    public void Delete(int id)
    {
    }
至于你的发帖方法

self.create = function (formElement) {
        $.ajax({
            url: '/api/Values',
            cache: false,
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(Command),
            success: function (data) {
              //Success function here.
            }
        }).fail(
                //Fail function here.
                 });

这里的javascript来自knockout.js方法,如果您需要完整的代码片段来了解如何使用knockout将其打包,请告诉我!但我希望这会让你朝着正确的方向前进

很抱歉,我无法理解您所说的选择的主要原因是我正在Visual Studio中开发多层解决方案。。你能解释一下吗?我认为你建议的第一个选择更适合我的目的。所以我决定在另一个文件夹中创建我的API。但是,如何从ActionResult中提取流程?我应该写什么?你能给我一个样品吗?如果这回答了你的问题,请标记为已回答!非常感谢。谢谢你的解释!没问题,我希望这能帮到你!
self.create = function (formElement) {
        $.ajax({
            url: '/api/Values',
            cache: false,
            type: 'POST',
            contentType: 'application/json; charset=utf-8',
            data: ko.toJSON(Command),
            success: function (data) {
              //Success function here.
            }
        }).fail(
                //Fail function here.
                 });