加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#:Asp.net WebApi到index.html的默认路由

发布时间:2020-12-15 08:17:28 所属栏目:百科 来源:网络整理
导读:我正在尝试创建一个 Asp.net WebApi /单页面应用程序.如果没有给出路由,我希望我的服务器分配index.html.我希望它以正常的“{controller} / {id}”方式指定一个控制器. 我意识到我可以使用http://localhost:555/index.html访问我的索引页面.如何通过访问http
我正在尝试创建一个 Asp.net WebApi /单页面应用程序.如果没有给出路由,我希望我的服务器分配index.html.我希望它以正常的“{controller} / {id}”方式指定一个控制器.

我意识到我可以使用http://localhost:555/index.html访问我的索引页面.如何通过访问http://localhost:555来做同样的事情?

解决方法

只需为您的WebApiConfig文件添加一个路径以获取索引页面.您的方法应如下所示:
public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Attribute routing
            config.MapHttpAttributeRoutes();

            // Route to index.html
            config.Routes.MapHttpRoute(
                name: "Index",routeTemplate: "{id}.html",defaults: new {id = "index"});

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读