我今天将我的项目升级到ASP.NET 4 RTM了ASP.NET MVC 2.0 RTM.
我以前用ASP.NET 3.5用ASP.NET MVC 2.0 RTM.
我的一些路线突然不起作用,我不知道为什么.我不确定是否在3.5和4.0之间发生了变化 - 或者这是否是4.0 RTM中的回归类型问题.(我以前从未用4.0测试过我的应用程序).
我喜欢使用它Url.RouteUrl("route-name", routeParams)来避免生成URL时出现歧义.这是我对画廊页面的路线定义.我想imageID成为可选项(如果你没有指定它,你会得到一个缩略图页面).
// gallery id
routes.MapRoute(
"gallery-route",
"gallery/{galleryID}/{imageID}/{title}",
new { controller = "Gallery", action = "Index",
galleryID = (string) null,
imageID = (string) null,
title = (string) null}
);
Run Code Online (Sandbox Code Playgroud)
在.NET 3.5/ASP.NET 2.0 RTM/IIS7中
Url.RouteUrl("gallery-route", new { galleryID = "cats"})
=> /gallery/cats
Url.RouteUrl("gallery-route", new { galleryID = "cats", imageID = "4")
=> /gallery/cats/4
Url.RouteUrl("gallery-route", …Run Code Online (Sandbox Code Playgroud) 我有一个ASP.NET MVC 3应用程序,它记录用户的计步器条目.用户可以通过访问查看所有最新计步器条目/Pedometer,并通过访问像URL可以按年,年/月,或年/月/日筛选/Pedometer/2011,/Pedometer/2011/08并/Pedometer/2011/08/15分别.
我在中创建了两个映射路由Global.asax.第一条路由(如下所示)允许各种URL模式按日期过滤.第二个路由(未显示)是默认的ASP.NET MVC路由.
routes.MapRoute(
"PedometerEntries", // Route name
"Pedometer/{year}/{month}/{day}", // URL with parameters
new
{
controller = "Pedometer",
action = "Index",
year = UrlParameter.Optional,
month = UrlParameter.Optional,
day = UrlParameter.Optional
}, // Parameter defaults
new
{
year = @"\d{4}",
month = @"([012]?\d{1})?",
day = @"(([1-9])|([12][0-9])|(3[0-1]))?"
} // Parameter constraints
);
Run Code Online (Sandbox Code Playgroud)
这是我的问题.我有一个视图,我想创建一个表单的链接:currentUrl?format=csv,这将允许用户以CSV格式下载所请求的URL的计步器条目.因此,如果用户正在访问/Pedometer,则下载链接将是/Pedometer?format=csv.如果用户正在访问,/Pedometer/2011/08则下载链接将是/Pedometer/2011/08?format=csv.
为了创建这样的链接,我添加了一个DownloadToExcel使用以下代码命名的自定义Html Helper :
public static …Run Code Online (Sandbox Code Playgroud) 我怎么能在控制器中创建一个方法,只是放一些参数,然后在点击表单提交后将其计算出来?在引擎盖下,它如何找到正确的方法,以及它如何确定我只是想要那些参数?
我可能期望从ASP.NET中获得太多,但在Apache中,重写URL非常简单,因此请求类似: http:// mysite/myfolder/mypage/niceurlparameter 实际上设法提供静态页面http://mysite/mypage.html
我如何在Global.asax中做到这一点?
我试过这个:
RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/*", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)
但是当我请求http:// mysite/myfolder/mypage/niceurlparameter时它会一直返回404 .
但是,这有效:
RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}.html/*", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)
所以我在申请http://mysite/myfolder/mypage.html/niceurlparameter时会得到mypage.html.
我只是想摆脱我的网址中的".html"部分.我错过了什么?
更新:由于某种原因,在前一种情况下,'*'通配符尚未被接受.
改为:
RouteTable.Routes.MapPageRoute("Static HTML", "myfolder/{page}/{whatever}", "~/myfolder/{page}.html");
Run Code Online (Sandbox Code Playgroud)
似乎将请求路由到html页面,但后来我收到错误:
There is no build provider registered for the extension '.html'.
Run Code Online (Sandbox Code Playgroud)
为什么在世界上它只适用于前一种情况(在URL中使用html),而不是在html被遗漏的情况下?
我需要为遗留应用程序添加一些新的生命:)
我想在请求"静态"HTML页面时调用MVC控制器,以便在将页面返回到客户端之前向页面添加一些标记.
我试着按照这个帖子中的方法:如何阅读.html页面中的web.config设置?
...但即使我定义了这条路线:
routes.MapRoute(
name: "Topic",
url: "html/{fileName}.html",
defaults: new { controller = "Topic", action = "Index" });
Run Code Online (Sandbox Code Playgroud)
控制器未被调用.我的web.config定义为:
<remove name="WebServiceHandlerFactory-Integrated" />
<add name="HTML" path="*.html" verb="*"
type="System.Web.UI.PageHandlerFactory"
resourceType="File" preCondition="integratedMode" />
Run Code Online (Sandbox Code Playgroud)
我怀疑除了PageHandlerFactory之外我还需要调用其他东西,或者问题可能完全不同.
更新:我的开发环境正在使用集成管道模式,但我需要检查我的生产环境是否支持它.
我正在尝试使用system.web.routing在路由表中创建一个路由到虚拟项目的路由(使用创建url的cms,例如example.com/about/company,其中没有名为company的物理文件存在)(遗憾的是)我不能使用iis重写/路由).我尝试了以下但它导致404.如果我指向另一个物理文件(测试目的),路由工作正常.
void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.MapPageRoute("about", "about/us", "~/about/company", false);
}
Run Code Online (Sandbox Code Playgroud)
那么,有可能指向这样的项目吗?
在我的网站中,我定义了以下路线:
routes.MapRoute(
name: "Specific Product",
url: "product/{id}",
defaults: new { controller = "", action = "Index", id = UrlParameter.Optional }
);
Run Code Online (Sandbox Code Playgroud)
这样我希望客户能够添加产品的ID并转到产品页面.
SEO顾问说,如果我们可以在URL上添加产品描述(如产品名称或其他内容)会更好.所以URL应该类似于:
/产品/我 - 冷 - 产品名称/ 123
要么
/产品/我 - 冷 - 产品名称-123
当然描述存储在db中,我不能用url重写(或者我可以吗?)
我应该在我的控制器上添加重定向(这似乎可以完成工作,但感觉不对)
在我检查的几个网站上,他们确实回复了一个301 Moved Permanently.这真的是最好的方法吗?
UPDATE
根据 Stephen Muecke我的评论,我检查了SO上发生了什么.
建议的网址是我自己使用路由操纵网址,我打开控制台查看任何重定向.这是一个截图:

在 ASP.NET MVC 4 中,我可以使用以下方法忽略某些路由:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{*old}", new {old=@"previous(/.*)?"});
}
Run Code Online (Sandbox Code Playgroud)
.NET Core 没有IgnoreRoute. 有没有办法在.NET Core 中实现这一点?也许一些自定义中间件?
asp.net asp.net-mvc asp.net-routing asp.net-core-mvc asp.net-core
我有一个使用 .NET Framework 4.7.1 的应用程序,其中定义了 WebAPI 和 MVC 路由。它们都在使用 IISExpress 进行本地调试时工作,但是当我部署到开发服务器(带有 IIS 8.5 的 Windows Server 2012R2)时,我得到的只是每个路由的 404.0 错误。我在 IIS 应用程序的根文件夹中放置了一个静态 test.aspx 文件,该文件返回正确,因此 IIS 似乎只是没有选择路由。我已经尝试了所有可以谷歌的东西,但没有任何东西可以修复它。
这是我的 RouteConfig.cs 代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 WebApiConfig.cs 代码:
using System.Web.Http;
using System.Net.Http.Formatting;
public …Run Code Online (Sandbox Code Playgroud) asp.net asp.net-mvc asp.net-routing asp.net-web-api2 iis-8.5
我有一个 ASP.NET Core 5 Web API,其中路由不匹配。它在本地工作正常,但在服务器上不起作用。日志告诉我
没有找到请求路径的候选项
但我看到请求路径并且它是正确的。
我想记录所有路线,只是为了看看我的应用程序认为这些路线是什么。查看服务器上的不同之处可能会有所帮助。
asp.net-routing ×10
asp.net-mvc ×7
asp.net ×5
asp.net-core ×2
.net ×1
.net-5 ×1
asp.net-4.0 ×1
c# ×1
iis-8.5 ×1
routing ×1
url-routing ×1