路由如何知道文件的位置?

时间:2022-02-23 08:08:30

My VS project has the following folder and files:

我的VS项目有以下文件夹和文件:

~\Controllers
  \AccountController.cs
  \HomeController.cs
...
~\Data
  \AccountController.cs

...
~\App_Start
  \RouteConfig.cs
  \WebApiConfig.cs

WebApiConfig.cs contains:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
     }
}

~\Data\AcccountController.cs contains:

namespace myApp.Data
{
    public class AccountController : ApiController
    {
        [HttpGet]
        public string GetUser(int id)
        {
            //...
        }
    ...
    }
}

When I make a http call to /api/Account/GetUser, the call is routed to the GetUser method shown above. What in all of the above or any configuration file tells the server to take the action from this particular file? What if ~/Controllers/AccountController.cs also contain a method of the same name?

当我对/ api / Account / GetUser进行http调用时,调用将被路由到上面显示的G​​etUser方法。所有上述或任何配置文件中的内容都告诉服务器从此特定文件中执行操作?如果〜/ Controllers / AccountController.cs还包含一个同名的方法怎么办?

1 个解决方案

#1


0  

It is called convention over configuration.

它被称为约定优于配置。

#1


0  

It is called convention over configuration.

它被称为约定优于配置。