我们可以创建一个自定义uri来调用不同的控制器方法函数

时间:2022-01-21 03:18:29

I have bee going through various MVC tutorials and have also made a simple one. All of them only seem to address two kinds of uri to use. What if we want to use something other than 'controller' and 'id'?

我已经完成了各种MVC教程并且做了一个简单的教程。所有这些似乎只能解决两种使用的uri问题。如果我们想要使用'controller'和'id'以外的东西怎么办?

also how does it know which function is being called? the uri is /api/products but the function that returns value is public IEnumerable<Product> GetAllProducts()

它如何知道调用哪个函数? uri是/ api / products但是返回值的函数是public IEnumerable GetAllProducts()

Below is an example of a controller

以下是控制器的示例

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);
        }
}

In this case I can only call the two functions in my web browser

在这种情况下,我只能在我的Web浏览器中调用这两个函数

我们可以创建一个自定义uri来调用不同的控制器方法函数

I wish to call uri that is somthing along the line of

我想打电话给uri这是一个沿着这条线的东西

/api/products/foo

3 个解决方案

#1


2  

You can use Route Prefixes and Route attributes to acceive it, like below:

您可以使用Route Prefixes和Route属性来加入它,如下所示:

[RoutePrefix("api/products")]
public class ProductsController : ApiController
{
    [Route("foo")]
    [Route("")]
    public IEnumerable<Product> GetAllProducts(){}
}

Then you can call /api/products/foo or /api/products, both of them give you same result.

然后你可以调用/ api / products / foo或/ api / products,它们都会给你相同的结果。

For more details about Web Api Attribute Routing, take a look at the following link: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

有关Web Api属性路由的更多详细信息,请查看以下链接:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web -API -2-

#2


2  

You should about Routing in ASP.NET MVC. Routing enables you to set your custom rules of defining which URLs should point to which actions (and controllers).

您应该在ASP.NET MVC中进行路由。路由使您可以设置自定义规则,以定义哪些URL应指向哪些操作(和控制器)。

You can read more about routing e.g. here: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

您可以阅读有关路由的更多信息这里:http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

#3


1  

change this

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

to

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

#1


2  

You can use Route Prefixes and Route attributes to acceive it, like below:

您可以使用Route Prefixes和Route属性来加入它,如下所示:

[RoutePrefix("api/products")]
public class ProductsController : ApiController
{
    [Route("foo")]
    [Route("")]
    public IEnumerable<Product> GetAllProducts(){}
}

Then you can call /api/products/foo or /api/products, both of them give you same result.

然后你可以调用/ api / products / foo或/ api / products,它们都会给你相同的结果。

For more details about Web Api Attribute Routing, take a look at the following link: http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

有关Web Api属性路由的更多详细信息,请查看以下链接:http://www.asp.net/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web -API -2-

#2


2  

You should about Routing in ASP.NET MVC. Routing enables you to set your custom rules of defining which URLs should point to which actions (and controllers).

您应该在ASP.NET MVC中进行路由。路由使您可以设置自定义规则,以定义哪些URL应指向哪些操作(和控制器)。

You can read more about routing e.g. here: http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

您可以阅读有关路由的更多信息这里:http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs

#3


1  

change this

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

to

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