Tag: asp.net mvc routing

如何在MVC路由中启用特殊字符?

我正在使用asp.net MVC 4。 这些是我的路线: routes.MapRoute( name: “Default”, url: “{controller}/{action}/{id}” ); 我当前的控制器正确响应以下请求: http://localhost:2020/PrivacyUrls/Details/ct14524 如何validation这些url? http://localhost:2020/PrivacyUrls/Details/*ct14524 http://localhost:2020/PrivacyUrls/Details/&ct14524 现在返回404。 从客户端(*)检测到潜在危险的Request.Path值。 从客户端(&)检测到潜在危险的Request.Path值。 我想添加这条路线,但它没有帮助: routes.MapRoute( “PivacyUrl/Details”, “PrivacyUrls/Details/{*ctid}”,// URL with parameters new { controller = “PrivacyUrls”, action = “Details” } );

如何确定任意URL是否与定义的路由匹配

如何判断字符串是否与特定的命名路由匹配? 我有这样的路线: routes.MapRoute( “FindYourNewRental”, “find-your-new-rental/{market}/{community}.html”, new { controller = “FindYourNewRental”, action = “Community” } ); string url = “http://www.website.com/find-your-new-rental/northerncalifornia/sacramento.html” 如何以编程方式判断’url’字符串是否与该路由匹配? 像这样的东西: // matches url with the named route “FindYourNewRental” if (IsRouteMatch(url, “FindYourNewRental”)) { // do something } public bool IsRouteMatch(string url, string routeName) { // How do I code this function }

在ASP.NET 5(vNext)MVC 6中添加自定义IRouter

我正在尝试将此示例RouteBase实现转换为与MVC 6一起使用。我已经通过遵循路由项目中的示例来解决大部分问题,但是我正在了解如何从方法返回异步Task 。 我真的不在乎它是否实际上是异步的(对能够提供该答案的任何人欢呼),现在我只想让它运转起来。 我有传出路线function(意味着当我输入路线值时, ActionLink工作正常)。 问题出在RouteAsync方法上。 public Task RouteAsync(RouteContext context) { var requestPath = context.HttpContext.Request.Path.Value; if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == ‘/’) { // Trim the leading slash requestPath = requestPath.Substring(1); } // Get the page that matches. var page = GetPageList() .Where(x => x.VirtualPath.Equals(requestPath)) .FirstOrDefault(); // If we got back a null value set, […]

可以在Asp.Net Web Api路由中使用句点吗?

我正在努力从原始的http处理程序移动API项目,我在路径中使用句点: http://server/collection/id.format 我想在Web Api(自托管)版本中遵循相同的URL架构,并尝试这样做: var c = new HttpSelfHostConfiguration(b); c.Routes.MapHttpRoute( name: “DefaultApiRoute”, routeTemplate: “{controller}/{id}.{format}”, defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional }, constraints: null ); 不幸的是,这似乎没有解决(在/ foo,/ foo / bar和/foo/bar.txt上一致的404’)。 在’format’之前使用斜杠的类似模式工作正常: var c = new HttpSelfHostConfiguration(b); c.Routes.MapHttpRoute( name: “DefaultApiRoute”, routeTemplate: “{controller}/{id}/{format}”, defaults: new { id = RouteParameter.Optional, format = RouteParameter.Optional }, constraints: null […]

当URL中有尾随空格时,WebAPI路由404

使用默认的Web api路由 config.Routes.MapHttpRoute( name: “API Default”, routeTemplate: “api/{controller}/{id}”, defaults: new { id = RouteParameter.Optional } ); 和控制器 public class TestController : ApiController { [HttpGet] public HttpResponseMessage Get(string id) { return Request.CreateResponse(HttpStatusCode.OK, id); } } 请求’api/test/1′ 返回1 如果由于某种原因你向’api/test/1%20’发送请求 路线404的。 现在这个例子看起来很愚蠢,因为浏览器会修剪尾随空格,但是 对于’api/{controller}/{id}/{extrastuff}’这样的路线 ‘1 ‘的空格将转换为’1%20’并且在未找到的路线上请求将为404。

ASP.Net MVC RouteData和数组

如果我有这样的动作: public ActionResult DoStuff(List stuff) { … ViewData[“stuff”] = stuff; … return View(); } 我可以使用以下url点击它: http://mymvcapp.com/controller/DoStuff?stuff=hello&stuff=world&stuff=foo&stuff=bar 但在我的ViewPage中,我有这个代码: 不幸的是,MVC不够聪明,无法识别该动作采用数组,并展开列表以形成正确的URL路由。 相反,它只是在对象上执行.ToString(),它只列出了List中的数据类型。 当其中一个目标Action的参数是数组或列表时,有没有办法让Html.ActionLink生成一个正确的URL? – 编辑 – 正如Josh在下面指出的那样,ViewData [“stuff”]只是一个对象。 我试图简化问题,但引起了一个无关的错误! 我实际上使用的是专用的ViewPage ,因此我有一个紧密耦合的类型感知模型。 ActionLink实际上看起来像: 其中ViewData.Model.Stuff被键入为List

ASP.NET MVC – 路由 – 具有文件扩展名的操作

有没有办法实现调用URL http://mywebsite/myarea/mycontroller/myaction.xml这基本上“假”请求文件但结果将是一个动作操作,将服务于动态创建的文件? 我试过这个: context.MapRoute( “Xml_filename”, “Xml/{controller}/{action}.xml” ); 但是,只要URL中存在filextension,路由就会失败并且表现就像我直接请求文件一样。 我怀疑这可能是因为使用扩展名更少的url处理程序。 谢谢你的任何建议。 的Jakub

ASP.NET MVC路由 – 向路由添加.html扩展名

我是MVC和路由的新手,我被要求修改应用程序以使用不同的URL。 因为我没有经验,所以有点超过我的任务。 好吧,让我们谈一下代码: routes.MapRoute( “CategoryBySeName”, // Route name “products/{SeName}”, // URL with parameters new { controller = “Catalog”, action = “CategoryBySeName” } ); 这按预期工作,但客户端在路径的末尾想要“.html”,所以我改变了: “products/{SeName}”, // URL with parameters 至: “products/{SeName}.html”, // URL with parameters 失败(IIS 404页面 – MapRequestHandler)似乎iis正在尝试加载具有该名称的物理文件,而不是将其传递给应用程序。 类似: ASP.NET MVC路由从html页面开始 (未回答,不重复)

mvc Html.BeginForm不同的URL架构

我正在为这样的DropDown创建一个表单: @{ Html.BeginForm(“View”, “Stations”, FormMethod.Get); } @Html.DropDownList(“id”, new SelectList(ViewBag.Stations, “Id”, “Name”), new { onchange = “this.form.submit();” }) @{ Html.EndForm(); } 如果我从我的下拉列表中选择一个值,我会被重定向到正确的控制器,但URL不是我想要的: /站/视图?ID = f2cecc62-7c8c-498d-b6b6-60d48a862c1c 我想要的是: /站/查看/ f2cecc62-7c8c-498d-b6b6-60d48a862c1c 那么如何将id = querystring参数替换为我想要的更简单的URL方案呢?

登录后如何更改路由到用户名?

在用户登录之前,路由是: localhost:54274/Home localhost:54274/Home/About localhost:54274/Home/Contact localhost:54274/Home/Login localhost:54274/Home/Register 用户登录后,路由为: 1. localhost:54274/Project 2. localhost:54274/Project/Create 3. localhost:54274/Project/Edit/1 4. localhost:54274/Project/Delete/2 5. localhost:54274/Project/1/Requirement 6. localhost:54274/Project/1/Requirement/Create 7. localhost:54274/Project/1/Requirement/Edit/3 8. localhost:54274/Project/1/Requirement/Delete/4 我想在用户登录后将路由更改为用户名。例如,用户名为hendyharf。 1. localhost:54274/hendyharf/Project 2. localhost:54274/hendyharf/Project/Create 3. localhost:54274/hendyharf/Project/Edit/1 4. localhost:54274/hendyharf/Project/Delete/2 5. localhost:54274/hendyharf/Project/1/Requirement 6. localhost:54274/hendyharf/Project/1/Requirement/Create 7. localhost:54274/hendyharf/Project/1/Requirement/Edit/3 8. localhost:54274/hendyharf/Project/1/Requirement/Delete/4 我的项目的控制器只有3个控制器: HomeController , ProjectController和RequirementController 我的RouteConfig.cs仍处于默认状态 public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute( name: “Default”, […]