网址重写/路由和网络API

时间:2014-03-08 08:59:12

标签: asp.net iis asp.net-web-api

我正在使用webapi设计一个基于主题的网站。我已将主题保存在文件夹A, BC中。主题是实际的html页面,根据客户端从数据库中获取一些内容(如照片)等。

所以我的实际页面如下:localhost/A/index.html?client=Client1(其中A是主题名称)。

我想将用户的网址显示为localhost/client1/index.html。我可以从客户端的id(示例中为client1)中获取主题名称。

  • 我应该使用什么样的路由和网址重写来完成此任务?
    我在IIS托管?我应该怎么做?

1 个答案:

答案 0 :(得分:0)

您可以拥有以下自定义路线:

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "CustomRoute",
        routeTemplate: "api/{client}/index.html",
        defaults: new { controller = "values" }
    );

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

和相应的API控制器:

public class ValuesController : ApiController
{
    // GET /api/{client}/index.html
    public HttpResponseMessage Get(string client)
    {
        // The client variable will represent the value
        // that was passed in the route segment
        return Request.CreateResponse(HttpStatusCode.OK, client);
    }
}
相关问题