如果我导航到以下stackoverflow URL,http://stackoverflow.com/questions/15532493它会自动附加问题的标题,如下所示:
http://stackoverflow.com/questions/15532493/mvc-custom-route-gives-404
也就是说,我可以在没有问题标题的情况下在我的浏览器中输入URL,并自动附加.
我如何在我的应用程序中实现相同的结果?(注意:我知道问题标题不会影响呈现的页面).
我有一个调用控制器,调用Users一个动作方法Details.我定义了以下路线:
routes.MapRoute("UserRoute",
"Users/{*domain}",
new { controller = "User", action = "Details" },
new { action = "^Details$" });
Run Code Online (Sandbox Code Playgroud)
由于这是一个Intranet应用程序,因此用户将根据其Windows帐户进行身份验证.我想将域名和用户名附加到URL.
如果我在视图中生成URL,如下所示:
@Html.ActionLink("Model.UserName", "Details", "User", new { domain = Model.Identity.Replace("\\", "/") })
Run Code Online (Sandbox Code Playgroud)
我得到一个如下所示的网址:
域/用户/ ACME/JSMITH
但是,如果用户Domain/Users/使用浏览器导航栏导航到URL ,则它与路径匹配,用户将进入用户详细信息页面.ACME/jsmith/在这种情况下,我想将其附加到URL上.
到目前为止我所做的研究表明我可能必须通过派生RouteBase和实现GetRouteData和GetVirtualPath方法来实现自定义路由对象,但我不知道从哪里开始(msdn documentaiton非常薄).
所以我想知道的是:
我最近似乎遇到的一个主要障碍是了解我一直在开发的一些基于MVC的应用程序的一些更复杂的路由要求.我在找到正确的教程集时遇到问题,让我通过它来获得完整的理解.
我想要找到的是一套完整的教程,用于从基本(控制器/动作/ id)到高级的所有路由.
我称之为高级路由的一个例子是:
/blog/year/month/day/title-将映射到控制器:blog与行动:post和参数:year,month,day和title
/blog/title- 将映射到controller:blog和action:post并作为参数:title
/title- 将映射到controller:blog和action:post并作为参数:title
我可以使用数据库将每个可能的集合映射到全局中的显式路由,但这似乎正在使得路由引擎路由到正确的位置失败了.我宁愿定义规则一次.
我们的工作室正在将ASP.NET MVC集成到一个大型Web应用程序中,该应用程序利用在system.webServer\handlers下的web.config中定义的自定义和第三方HTTP处理程序.以这种方式利用HTTP处理程序对我们来说非常好,因为我们不需要重新编译应用程序,也不需要将实际处理程序页面放在Web范围内的磁盘上,以用于每个产品实例.
是否真的有必要在我们的global.asax中添加显式的忽略路由,以便运行时可以遵守web.config中定义的处理程序?我认为在检查了system.webServer\handlers中定义的处理程序之后会调用Web.Routing (而不是相反).
我们使用模块化设计,允许在添加功能时从web.config添加/删除处理程序.随着MVC路由的引入,我们需要在global.asax文件中为web.config中定义的每个可能的处理程序添加忽略路由.
请注意,磁盘上不存在这些处理程序的实际文件 - 它们是虚拟的并嵌入在程序集中.以下是第三方处理程序的示例,现在需要在global.asax中显式忽略路由:
<system.webServer>
<handlers>
<!-- telerik radcontrols -->
<add name="TelerikDialogHandler" verb="*" path="Telerik.Web.UI.DialogHandler.aspx" type="Telerik.Web.UI.DialogHandler, Telerik.Web.UI, Version=2009.1.402.20, Culture=neutral, PublicKeyToken=121fae78165ba3d4"></add>
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
因此,对于记录,如果您使用System.Web.Routing,则必须包含在Web.Config中指定的Http Handler的忽略路由?或许我做错了什么?
我在Microsoft Visual Studio Express 2013 for Web中创建了一个新项目.它是一个ASP.NET MVC 5 - .NET Framework 4.5项目.
我想处理(无法找到资源):

我确实使用下面的代码处理它.
如果我执行类似(/ Home/kddiede/ddiij)或(/ djdied/djie/djs)的操作,此代码将起作用,这将导致显示我的自定义错误页面.
但是,当我尝试执行类似(/ Home/kddiede/ddiij/dfd/sdfds/dsf/dsfds/fd)或任何长的不存在的URL时,它会向我显示:

代码来自:http://www.codeproject.com/Articles/635324/Another-set-of-ASP-NET-MVC-4-tips
技巧16:自定义错误屏幕
错误页面位于/View/Shared/Error.cshtml中
Web.config文件
<system.web>
<customErrors mode="RemoteOnly" />
</system.web>
Run Code Online (Sandbox Code Playgroud)
Global.asax中
protected void Application_EndRequest(Object sender, EventArgs e)
{
ErrorConfig.Handle(Context);
}
Run Code Online (Sandbox Code Playgroud)
ErrorConfig类
public class ErrorConfig
{
public static void Handle(HttpContext context)
{
switch (context.Response.StatusCode)
{
//Not authorized
case 401:
Show(context, 401);
break;
//Not found
case 404:
Show(context, 404);
break;
}
}
static void Show(HttpContext context, Int32 code)
{
context.Response.Clear(); …Run Code Online (Sandbox Code Playgroud) 注意:此代码中没有MVC.纯旧的Web表单和
.asmxWeb服务.
我.asmx在我的新公司继承了一个大规模的ASP.NET Web Forms和Web Service()应用程序.
由于某些需要,我正在尝试为所有Web窗体执行URL路由,这是我成功完成的.
现在.asmx,routes.MapPageRoute不起作用.根据下面的文章,我创建了一个IRouteHandler类.以下是代码的外观:
using System;
using System.Web;
using System.Web.Routing;
using System.Web.Services.Protocols;
using System.Collections.Generic;
public class ServiceRouteHandler : IRouteHandler
{
private readonly string _virtualPath;
private readonly WebServiceHandlerFactory _handlerFactory = new WebServiceHandlerFactory();
public ServiceRouteHandler(string virtualPath)
{
if (virtualPath == null)
throw new ArgumentNullException("virtualPath");
if (!virtualPath.StartsWith("~/"))
throw new ArgumentException("Virtual path must start with ~/", "virtualPath");
_virtualPath = virtualPath;
}
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// Note: can't pass …Run Code Online (Sandbox Code Playgroud) 我有这样的路线:
routes.MapRoute
(
"Profile",
"profile/{username}",
new { controller = "Profile", action = "Index" },
new { username = @"^(getmoreactivity)" }
);
Run Code Online (Sandbox Code Playgroud)
这适用于所有用户,但我有一种情况,我想要针对getmoreactivity点击一个动作.所以我想制定这个约束来说明当用户名不是getmoreactivity时.它只是没有工作.
我坚持使用RouteDebugger并尝试@"[^(getmoreactivity)]"@"^^(getmoreactivity)"@"^ getmoreactivity"@"[^ getmoreactivity]".好吧,我尝试了无数的东西,但没有解决我的问题.
你到底怎么对整个单词施加NOT约束?
asp.net asp.net-mvc routing asp.net-routing asp.net-mvc-routing
我看到人们在webform中路由时使用星号.我只是不明白Asterisk标志的重要性如下
routes.MapPageRoute(
"View Category", // Route name
"Categories/{*CategoryName}", // Route URL
"~/CategoryProducts.aspx" // Web page to handle route
);
Run Code Online (Sandbox Code Playgroud)
星号标志的含义是什么,也告诉我我应该使用上面的星号标志是什么样的情况.
"Categories/{*CategoryName}"
Run Code Online (Sandbox Code Playgroud)
如果有人使用Asterisk标志的小样本代码只是为了显示真实应用程序中星号的重要性和使用,那会更好.
403.14 Forbidden当通过ASP.NET URL路由处理的URL恰好对应于ASP.NET项目中的物理文件夹时,IIS Express会产生错误.(该文件夹仅包含代码,并且巧合的是文件夹名称恰好与页面的URL匹配;我的URL结构由数据库动态确定,用户可以编辑该结构,所以尽管我可以重命名我的项目文件夹,一般来说,我无法阻止这种碰撞发生.)
这似乎正在发生,因为DirectoryListingModule处理请求的步骤,然后迅速失败,因为目录浏览被禁用.我试过删除这个:
<system.webServer>
<handlers>
<remove name="StaticFile" />
<add name="StaticFile" path="*" verb="*"
modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
这将删除默认的StaticFile处理程序配置,该配置具有modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule",并将其替换为仅提供我想要的功能的配置.(我想要静态文件服务,但我不需要在这个应用程序中的目录列表或默认文档.)但效果似乎是当我点击时IIS然后产生一个完全空(0字节)响应(具有200状态)违规的页面.
接下来,我尝试配置StaticFile处理程序以仅处理我想要提供的特定物理文件夹:
<system.webServer>
<handlers>
<remove name="StaticFile" />
<add name="StaticFileCss" path="style/*.css" verb="*"
modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
<add name="StaticFileScripts" path="Scripts/*" verb="*"
modules="StaticFileModule" resourceType="Either" requireAccess="Read" />
</handlers>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)
但是当我点击有问题的URL时,会产生404.4 - Not found错误,并显示消息The resource you are looking for does not have a handler associated with it..(错误页面上的详细错误信息表明我们在IIS Web Core模块中,在MapRequestHandler …
我最近将 webapi 升级到 .NET 8。当我测试控制器时,ApiVersion 属性在 swagger 页面中不起作用。
[ApiVersion("1.0")]
[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
[Authorize]
public class SubscriptionController(ISubscriptionDataService subscriptionDataService,
IConfigurationService configurationService) : ControllerBase
Run Code Online (Sandbox Code Playgroud)
在 swagger 页面中,默认情况下不会填写版本,就像在 .NET 7 中一样。
以前,使用默认模板weatherforecast 控制器时,行为是默认填充的。
该控制器的代码如图所示。
[ApiVersion("1.0")]
[Route("v{version:apiVersion}/api/[controller]")]
[ApiController]
[Authorize]
public class WeatherForecastController : ControllerBase
Run Code Online (Sandbox Code Playgroud)
在 Program.cs 中,添加以下内容以执行版本控制。
private static void AddApiVersioning(IServiceCollection services)
{
services.AddApiVersioning(options =>
{
options.AssumeDefaultVersionWhenUnspecified = false;
options.ReportApiVersions = false;
});
services.AddVersionedApiExplorer(options =>
{
options.GroupNameFormat = "'v'VVV";
options.SubstituteApiVersionInUrl = true;
});
}
Run Code Online (Sandbox Code Playgroud)
编辑1
由于以下弃用,完成此操作的方式需要更改。
Microsoft.AspNetCore.Mvc.Versioning -> Asp.Versioning.Mvc Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer -> Asp.Versioning.Mvc.ApiExplore
在Program.cs中,需要完成以下操作:
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApiVersioning(options …Run Code Online (Sandbox Code Playgroud) asp.net-routing ×10
asp.net-mvc ×4
asp.net ×3
c# ×3
url ×2
.net-8.0 ×1
httphandler ×1
iis ×1
iis-7 ×1
ip-address ×1
routes ×1
routing ×1
web-config ×1
web-services ×1
webforms ×1