如何在 ASP.NET MVC 中处理空 URL
如何设置路由来处理 http://mysite.com/Foo?
其中 foo 可以是任何值。 我希望它转到 /Home/Action/Id,其中 foo 是 id。
这些是我尝试过的映射:
routes.MapRoute("Catchall", "{*catchall}",
new { controller = "Home", action = "Index", id=""});
routes.MapRoute("ByKey", "{id}",
new { controller = "Home", action = "Index" id=""});
它们都生成了 404。
感谢您的帮助。
How do you set up the routing to handle http://mysite.com/Foo?
Where foo can be any value. I want it to go to /Home/Action/Id where foo is the id.
These are the mappings I have tried:
routes.MapRoute("Catchall", "{*catchall}",
new { controller = "Home", action = "Index", id=""});
routes.MapRoute("ByKey", "{id}",
new { controller = "Home", action = "Index" id=""});
They both generated a 404.
Thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个(注意你原来的帖子中缺少逗号):
但是,我会使其更具解释性,以防止以后发生冲突,即使这意味着更长的 URI:
并将其作为第一个 MapRoute 命令。 那里的顺序确实很重要,您添加的第一个路由是要测试的 URL 的第一个路由。
Try this (note you had a missing comma in your original post):
I would, however, make it a bit more explanatory to prevent clashes later, even if it means a bit longer URIs:
And place this as the first MapRoute command. Order does matter there, and the first route you add is the first route for a URL to be tested with.
你的第二条路线是正确的。 它应该有效。 也许您还有上面的其他路线? 使用 Phil Haack 的 ASP.NET 路由调试器
Your second route is correct. It should work. Maybe you have another routes above? Debug your routes with Phil Haack's ASP.NET Routing Debugger
解决步骤
右键单击该项目
转到页面的 Web 选项卡。
发现我的开始 URL 为空
我复制了服务器部分的项目 url 中的内容
粘贴到开始网址
它起作用了。
Steps to solve
Right click on the project
Go to web tab of the page.
My Start URL was found to be empty
I copied what in Project url of Servers section
Pasted at Start Url
It worked.