簡體   English   中英

Web API路由,URL不起作用

[英]Web api routing, URL not working

我目前正在嘗試為自己制作的加密貨幣跟蹤器創建自己的Web-Api。

我想從數據庫中獲取值。 這里的技術是MVC5。

我有一個數據庫,其中包含我的錢包值以及附加的時間/日期,然后在這里有一個api方法:

namespace Crytocurrency_Web___Main.Controllers
{
[RoutePrefix("WalletValue")]
public class WalletValueController : ApiController
{
    readonly ApplicationDbContext dbContext = new ApplicationDbContext();

    [Route("GetAllValues")]
    [HttpGet]
    public List<WalletValue> GetAllValues()
    {
        return dbContext.Wallet.ToList();
    }
  }
}

但是當我轉到地址localhost:51833 / WalletValue / GetAllValues時,出現錯誤:

Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its 
dependencies) could have been removed, had its name changed, or is temporarily unavailable.  Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /WalletValue/GetAllValues

這也是路由配置文件:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }

這是控制器存儲位置的文檔明細

解決方案->控制器-> WalletValueController

支架式WebAPi:

  public class WalletValuesController : ApiController
{
    private ApplicationDbContext db = new ApplicationDbContext();

    // GET: api/WalletValues
    public IQueryable<WalletValue> GetWallet()
    {
        return db.Wallet;
    }

    // GET: api/WalletValues/5
    [ResponseType(typeof(WalletValue))]
    public IHttpActionResult GetWalletValue(int id)
    {
        WalletValue walletValue = db.Wallet.Find(id);
        if (walletValue == null)
        {
            return NotFound();
        }

        return Ok(walletValue);
    }

    // PUT: api/WalletValues/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutWalletValue(int id, WalletValue walletValue)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != walletValue.Id)
        {
            return BadRequest();
        }

        db.Entry(walletValue).State = EntityState.Modified;

        try
        {
            db.SaveChanges();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!WalletValueExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

    // POST: api/WalletValues
    [ResponseType(typeof(WalletValue))]
    public IHttpActionResult PostWalletValue(WalletValue walletValue)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        db.Wallet.Add(walletValue);
        db.SaveChanges();

        return CreatedAtRoute("DefaultApi", new { id = walletValue.Id }, walletValue);
    }

    // DELETE: api/WalletValues/5
    [ResponseType(typeof(WalletValue))]
    public IHttpActionResult DeleteWalletValue(int id)
    {
        WalletValue walletValue = db.Wallet.Find(id);
        if (walletValue == null)
        {
            return NotFound();
        }

        db.Wallet.Remove(walletValue);
        db.SaveChanges();

        return Ok(walletValue);
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

    private bool WalletValueExists(int id)
    {
        return db.Wallet.Count(e => e.Id == id) > 0;
    }

您的Route屬性未使用。 您必須使用以下命令(在WebApiConfig.cs中)為Web API啟用屬性路由:

public static class WebApiConfig {
    public static void Register(HttpConfiguration config) {
        // Web API routes
        config.MapHttpAttributeRoutes();
    }
}

您還可以添加此選項以為非API控制器啟用屬性路由(在RegisterRoutes方法中):

routes.MapMvcAttributeRoutes();

完成后,可以根據需要刪除route.MapRoute行。

那應該使它按您的方式工作。

但是請注意,您可以將[RoutePrefix("WalletValue")]放在WalletValueController ,然后只需在操作上放置[Route("GetAllValues")] 這使您不必將WalletValue/放在每個操作上。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM