iT邦幫忙

0

請問Vs2015 內使用[ApiController],要using 哪一些?

  • 分享至 

  • xImage

您好:
在Vs2015 內使用[ApiController]
有要去
https://shaurong.blogspot.com/2021/07/aspnet-cs0246-apicontroller-using.html

安裝,但都沒有反應
Microsoft.AspNet.WebApi.Client.5.2.7
Microsoft.AspNet.WebApi.Core.5.2.7
Microsoft.AspNet.WebApi.Core.zh-Hant.5.2.7

請問,ApiController 要手動using 那些?

https://ithelp.ithome.com.tw/upload/images/20240215/20104095UtmJmPwnRx.png

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace API01.Controllers
{
    [ApiController]
    [Route("[controller]")]

    public class WeatherForecastController : Controller
    {
        // GET: WeatherForecast
        private static readonly string[] Summaries = new[]
     {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild",
        "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        public IEnumerable<WeatherForecast> Get()
        {
            var rng = new Random();
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = rng.Next(-20, 55),
                Summary = Summaries[rng.Next(Summaries.Length)]
            })
            .ToArray();
        }



    }
}

補1:
重建好: 一樣是一堆ERR

https://ithelp.ithome.com.tw/upload/images/20240215/20104095kTKZPNR9bQ.png

VS2023 沒有Core ,只有.net framework
https://ithelp.ithome.com.tw/upload/images/20240215/20104095gVKD1Y41vC.png

https://ithelp.ithome.com.tw/upload/images/20240215/20104095ypw4Jx3rPy.png

Yaowen iT邦研究生 4 級 ‧ 2024-02-15 13:44:35 檢舉
你這個專案是哪一個版本的 .net core 3.1 or net 6 or net 8 ?
你重建一次專案就會自動 using 好了 不需要手動
noway iT邦研究生 3 級 ‧ 2024-02-15 15:19:53 檢舉
您好:
我的回複如上,再麻煩指導
謝謝
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

1
wjing
iT邦新手 1 級 ‧ 2024-02-16 10:23:59

看截圖你的專案是.net framework 4.5.2
.net framwork ApiController的寫法
參考官方的範例是這樣子寫,用繼承的方式 : ApiController

using ProductsApp.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;

namespace ProductsApp.Controllers
{
    public class ProductsController : ApiController
    {
        Product[] products = new Product[] 
        { 
            new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
            new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
            new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
        };

        public IEnumerable<Product> GetAllProducts()
        {
            return products;
        }

        public IHttpActionResult GetProduct(int id)
        {
            var product = products.FirstOrDefault((p) => p.Id == id);
            if (product == null)
            {
                return NotFound();
            }
            return Ok(product);
        }
    }
}

using System.Web.Http;
https://ithelp.ithome.com.tw/upload/images/20240216/20110063HBzr2kEmL4.png


[ApiController] 寫法是.net core的寫法
想用這種寫法的話,那得把專案類型改成.net core

noway iT邦研究生 3 級 ‧ 2024-02-19 15:56:07 檢舉

您好:
謝謝,就我原POST ,加using System.Web.Http;
繼承 : ApiController
還是一堆 紅底線
我先重新用您提供的範例 試試看

https://ithelp.ithome.com.tw/upload/images/20240219/20104095vpoDim8vNV.png

wjing iT邦新手 1 級 ‧ 2024-02-23 17:19:02 檢舉

截圖中 [ApiController]這行不需要了
其他紅色底線是其他錯誤,跟ApiController無關

我要發表回答

立即登入回答