【问题标题】:ApiController could not be found找不到 ApiController
【发布时间】:2020-08-04 18:17:56
【问题描述】:

基于此视频https://www.youtube.com/watch?v=IVvJX4CoLUY 我添加了 using System.Web;使用 System.Web.Http; 但我仍然得到 apicontroller 找不到的错误状态,等等,下图是我面临的错误:

下面是我的代码:

using System;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;
using System.Collections.Generic;


namespace UploadToServer.Server.Controllers
{
public class UploadsController : ApiController
{
    [Route("api/Files/Upload")]
    public async Task<string> Post()
    {
        try
        {
            var httpRequest = HttpContext.Current.Request;

            if (httpRequest.Files.Count > 0)
            {
                foreach (string file in httpRequest.Files)
                {
                    var postedFile = httpRequest.Files[file];

                    var fileName = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();

                    var filePath = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);

                    postedFile.SaveAs(filePath);

                    return "/Uploads/" + fileName;
                }
            }
        }
        catch (Exception exception)
        {
            return exception.Message;
        }

        return "no files";
    }
}

}

谁能分享我的想法?

【问题讨论】:

    标签: c# web-services xamarin xamarin.forms visual-studio-2017


    【解决方案1】:

    您的项目中缺少这些库的引用。根据svdoever,您可以通过 NuGet 添加包Microsoft.AspNet.WebApi 来解决此问题。 NuGet使用说明请点击here

    【讨论】:

      【解决方案2】:

      您的问题与 Xamarin.Forms 无关,而与 WEB.API 无关。您的 Xamarin.Forms 项目不能也不应该包含任何 WEB.Api 控制器。您的服务器端代码应该在一个单独的项目中。最简单的方法是使用特定模板创建一个单独的项目。

      幸运的是,Microsoft 有一个非常易于遵循的指南:
      https://docs.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

      不过,我建议您改用 Asp.NET Core:
      https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api

      【讨论】:

      • 嗨,我以 xamarin 形式应用这个
      • 您的 Xamarin.Forms 项目不能也不应该包含任何 WEB.Api 控制器。您的服务器端代码应该在一个单独的项目中。重新观看 youtube 视频。
      • 您好,我已经完全按照视频进行了操作,但现在我只收到 HttpContext 错误,有什么办法可以克服吗?
      • 我正在关注 Asp.NET 教程并且遇到了同样的问题。根据您的帖子,我不确定如何解决此错误。
      【解决方案3】:

      您收到此错误是因为未安装运行 API 所需的软件包。 您必须手动从 nuget.org 下载此包。

      按照以下步骤-

      1. 检查包源中是否列出了 Nuget.ORG。 工具 -> Nuget 包管理器 -> 包管理器设置 -> 包源。 如果 Nuget 未在包源中列出,则将源添加为“http://nuget.org”并更新。
      2. 工具 -> Nuget 包管理器 -> 包管理器控制台 选择包源为“Nuget”并点击恢复

      同样可以通过右键单击您的解决方案文件 -> 管理 Nuget 包来解决。 在 Package Source 中选择 Nuget 并恢复。

      将从 nuget.org 下载所需的包

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-07-20
        • 1970-01-01
        • 1970-01-01
        • 2012-10-05
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        相关资源
        最近更新 更多