Tag: 路由

ASP NET Web API中的路由 – 适用于不同版本的API

我正在从这里阅读Attribute Routing in Web API 2 文章说, Here are some other patterns that attribute routing makes easy. API versioning In this example, “/api/v1/products” would be routed to a different controller than “/api/v2/products”. /api/v1/products /api/v2/products 怎么会? 编辑:我会在正常路由中执行此操作: public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: “DefaultApi”, routeTemplate: “api/v2/products”, defaults: new { […]

在Webforms中生成带URL路由的URL

我知道在MVC框架中,你有Html类来创建URL: Html.ActionLink(“About us”, “about”, “home”); 但是如果你想在Webforms中生成Urls呢? 我没有找到关于使用Webforms生成URL的详细信息。 例如,如果我正在生成这样的路线: Route r = new Route(“{country}/{lang}/articles/{id}/{title}”, new ArticleRouteHandler(“~/Forms/Article.aspx”)); Route r2 = new Route(“{country}/{lang}/articles/”, new ArticleRouteHandler(“~/Forms/ArticlesList.aspx”)); Routes.Add(r); Routes.Add(r2); 如何使用路由表数据生成URL。 如何根据我的路线生成URL? 例如。 / ca /​​ en / articles / 123 /文章标题没有

使用RedirectToAction – MVC将信息传递给另一个动作

我有这个动作: public ActionResult Report(AdminReportRequest reportRequest, FormCollection formVariables) { AdminEngine re = new AdminEngine(); AdminReport report = re.GetCompleteAdminReport(reportRequest); return View(report); } 我想知道如何重定向到同一个控制器中的另一个动作,传递AdminReportRequest和FormCollection变量? 我有这样的想法: public ActionResult EarningsSalesReport(AdminReportRequest reportRequest, FormCollection formVariables) { if (!reportRequest.Download) { AdminEngine re = new AdminEngine(); AdminReport report = re.GetCompleteAdminReport(reportRequest); return View(report); } return RedirectToAction(“ExcelSalesReport”, reportRequest, formVariables); } public FileResult ExcelSalesReport(AdminReportRequest reportRequest, FormCollection formVariables) […]

Web api – 如何使用slugs进行路由?

我希望能够解析像这样的问题的链接: http://stackoverflow.com/questions/31223512/web-api-how-to-route-using-slugs 因此,服务器上的路由只是忽略 URL的最后部分。 作为使用这个问题的一个例子,如果有人输入这样的URL,我怎样才能正确实现路由,它将我重定向到: http://stackoverflow.com/questions/31223512

为什么在控制器上下文之外获取路由值如此困难?

我不明白这背后的交易是什么,为什么在控制器的Request中获取路由值如此容易,但几乎不可能在HttpContext.Current.Request上做同样的事情? 也许我只是不知道更好的方式而且存在。 有人可以确认这是获取控制器外部路径数据的唯一方法吗? 例 [Route(“{id}”), HttpGet] public IHttpActionResult Test() { // Simple and easy var route1 = Request.GetRouteData().Values[“id”]; // Wat. This is also ~6 times slower var routeValues = (IHttpRouteData[]) HttpContext.Current.Request.RequestContext.RouteData.Values[“MS_SubRoutes”]; var route2 = routeValues.SelectMany(x => x.Values).Where(x => x.Key == “id”).Select(x => x.Value).FirstOrDefault(); return Ok(route1 == route2); // true }

在不使用MVC更改URL的情况下为特定URL创建路由

我有一个在www.domain.com上运行的MVC Web应用程序,我需要为另一个域www.domain2.com为同一个Web应用程序配置不同的URL绑定。 新域名www.domain2.com必须返回特定的控制器操作视图,例如/Category/Cars : routes.MapRoute( name: “www.domain2.com”, url: “www.domain2.com”, defaults: new { controller = “Category”, action = “Cars”, id = UrlParameter.Optional } ); 如何在不更改URL的情况下实现此目的,以便访问者插入urlwww.domain2.com并收到视图www.domain.com/category/cars但url仍为www.domain2.com ? 编辑: 我尝试过这种方法,但它不起作用: routes.MapRoute( “Catchdomain2”, “{www.domain2.com}”, new { controller = “Category”, action = “Cars” } );

ASP.NET MVC 2中具有约束的可选路由参数?

如果我有这样的路线: routes.Add(new Route(“{controller}/{page}”, new RouteValueDictionary { { “page”, UrlParameter.Optional } }, new RouteValueDictionary { { “page”, @”[Pp]age\d+” } }, new MvcRouteHandler() )); 然后当{page}缺失时路线不匹配,但是如果我删除它匹配的约束。 这是一个错误还是一个function?

ActionLink包含URL中的当前{id}

我浏览器中当前显示的页面是: http://localhost:19255/Object/Browse/1 。 此页面上的链接创建时使用: @Html.ActionLink(“…”, “Browse”, “Object”) 但生成的链接实际上是: /Object/Browse/1 我对正在发生的事情的理解是MVC看到我的路线有{id}部分,我没有提供。 所以它继续并包含当前页面的{id}部分。 很公平,但如何在没有它的情况下创建链接? 我尝试了null , new { id = null }但是都没有用。

RouteData.Values保持为空

我的路线代码如下: RouteTable.Routes.MapPageRoute(“IDP”, “Person/{IDP}”, “~/Person.aspx”) 现在我想获取表单上的数据,通常它的工作原理如下: int id = Convert.ToInt32(Page.RouteData.Values[“IDP”]); 但每次我试图从路线获取数据,例如: http://PC-81/SkillDatenbank/Person/1我没有从值中获取数据(它是空的!) 我正在使用ASP.Net 4.5与Web窗体。 编辑:我做了一个新项目并进行了测试,它没有工作要么我做错了什么? 在上一个项目中它确实像这样工作:(你能帮帮我吗?

ASP.NET MVC 2中具有一个名称的多个控制器

尝试运行我的ASP.NET MVC应用程序时收到以下错误: 对’Account’的请求找到了以下匹配的控制器: uqs.Controllers.Admin.AccountController MvcApplication1.Controllers.AccountController 我在项目中搜索了MvcApplication1.Controllers.AccountController来删除它,但我找不到匹配项。 我尝试注册修复它的路线: routes.MapRoute( “LogAccount”, // Route name “{controller}/{action}/{id}”, // URL with parameters new { controller = “Account”, action = “LogOn”, id = “” }, new string[] { “uqs.Controllers.Admin” } // Parameter defaults ); 但这并没有解决它。 发现多个类型与名为“帐户”的控制器匹配。 我该如何解决这个问题?