如何更改asp.net网页API默认路由 [英] How to change default route in asp.net web api

查看:180
本文介绍了如何更改asp.net网页API默认路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的asp.net网页API。我想设置为我的项目的默认路由在Global.asax文件一样,

I am working on asp.net web api. I am trying to set the default route for my project in global.asax file like,

localhost:45678/api/Products

但我没有找到像

url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }

它总是重定向我主页(HomeController中)。请指导我。

It always redirects me to Home page(HomeController). please guide me.

推荐答案

检查你的 RouteConfig 类在 App_Start 文件夹中。您应该看到的默认路由下面,你可以改变。

Check your your RouteConfig class in your App_Start folder. You should see the default route below which you can change.

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

修改

阅读您的意见后,我觉得这个问题是不是与你的路由。我不知道你为什么会的希望的去做,但你只需要指定起始网址为您的项目。右键单击您的Web项目 - 点击属性 - 点击网络标签 - 在开始行动选择开始网址,然后输入 HTTP ://本地主机:45678 / API /产品在框中。保存项目并再次运行。它应该在新的位置开始。

After reading your comments, I think the problem is not with your routes. I'm not sure why you would want to do it, but you just need to specify the Start URL for your project. Right click your web project - click Properties - click the Web tab - under Start Action select Start URL and enter http://localhost:45678/api/Products in the box. Save your project and run again. It should start at the new location.

这篇关于如何更改asp.net网页API默认路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆