Tag: asp.net mvc routing

什么是ASP.NET MVC中的WebApi 等价物?

在WebApi中,我可以使用[FromUri]在控制器动作上装饰一个参数,以便将URI的组件’反序列化’(如果愿意的话)放入POCO模型中; 又称模型绑定。 尽管从2.0开始使用MVC,我从未将它用于网站(不知道为什么)。 ASP.NET MVC 5中它的等价物是什么? 除非我需要引用库,否则IDE中似乎无法识别该属性。 我想~/thing/2014/9绑定到下面的模型: public class WhateverModel { public int Year { get; set; } public int Month { get; set; } } 谢谢 更新 在另一个问题(上面的链接)中,OP说: 但是,将其切换为普通MVC而不是WebApi,并且默认模型绑定器会崩溃,并且无法绑定嵌套数组中对象的属性 这意味着他正在使用WebApi中的属性。 我猜。 我没有那些引用,因为我在MVC中,所以(ab)使用WebApi的版本是否可以在MVC中执行此操作? 更新2 在这个问题的答案是: 您需要构建关于MVC模型绑定器命名约定的查询字符串。 此外,示例操作中的[FromUri]属性被完全忽略,因为MVC DefaultModelBinder不知道它 因此,如果他在错误的属性上取得了一些成功,那么我仍然不知道OP在该问题上甚至在谈论什么做什么或地球上有什么。 我想我希望得到一个明确的答案,而不是其他问题的泥泞。

ASP.NET MVC控制器unit testing – UrlHelper扩展问题

尝试在我的ASP.NET MVC 3 Web应用程序中进行一些控制器unit testing。 我的测试是这样的: [TestMethod] public void Ensure_CreateReviewHttpPostAction_RedirectsAppropriately() { // Arrange. var newReview = CreateMockReview(); // Act. var result = _controller.Create(newReview) as RedirectResult; // Assert. Assert.IsNotNull(result, “RedirectResult was not returned”); } 很简单。 基本上测试[HttpPost]动作以确保它返回RedirectResult (PRG模式)。 我没有使用RedirectToRouteResult因为没有任何重载支持锚链接。 继续。 现在,我正在使用Moq来模拟Http Context,包括服务器变量,控制器上下文,会话等。到目前为止一切顺利。 直到我在我的动作方法中点击这一行: return Redirect(Url.LandingPageWithAnchor(someObject.Uri, review.Uri); LandingPageWithAnchor是一个自定义HTML帮助器: public static string LandingPageWithAnchor(this UrlHelper helper, string uri1, string uri2) […]

ASP.NET MVC 3路由

我正在尝试创建路线。 这是 /emlak/TITLE/number.aspx 如 /emlak/Here_is_your_best_property/123456.aspx Global.asax中: routes.MapRoute( “Product”, “{controller}/{deli}/{productId}”, new { controller = “emlak”, action = “Index” }, new { productId = UrlParameter.Optional , deli = UrlParameter.Optional } ); 我的控制器 namespace emrex.Controllers { public class EmlakController : Controller { // // GET: /Emlak/ public ActionResult Index(String productId, String deli) { return View(); } } } […]

从ActionFilterAttribute返回带有它的模型的视图

在强类型视图上使用内置validation助手实现error handling时,通常在控制器中创建一个try / catch块,并返回一个视图,其中相应的模型作为View()方法的参数: 控制器 public class MessageController : Controller { [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Models.Entities.Message message) { try { // Insert model into database var dc = new DataContext(); dc.Messages.InsertOnSubmit(message); dc.SubmitChanges(); return RedirectToAction(“List”); } catch { /* If insert fails, return a view with it’s corresponding model to enable validation helpers */ return View(message); } […]

System.Web.Routing.RouteCollection.GetRouteData中的exception

我在iis7上运行的asp.net mvc代码中随机获得了两个exception: Exception type: InvalidOperationException Exception message: Collection was modified; enumeration operation may not execute. at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource) at System.Collections.Generic.List’1.Enumerator.MoveNextRare() at System.Collections.Generic.List’1.Enumerator.MoveNext() at System.Web.Routing.RouteCollection.GetRouteData(HttpContextBase httpContext) at System.Web.Routing.UrlRoutingModule.PostResolveRequestCache(HttpContextBase context) at System.Web.Routing.UrlRoutingModule.OnApplicationPostResolveRequestCache(Object sender, EventArgs e) at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 和 Exception type: NullReferenceException Exception message: Object reference not set to an instance of an […]

MVC 6路由,SPA后备+ 404错误页面

使用ASP.NET Core 1.0的MVC 6的 RC1 ,您可以在调用app.UseMvc时从Startup.Configure函数中映射路由。 我已经映射了一个“spa-fallback”路由,它将确保HomeController和Index视图是默认值,如下所示: public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // … omitted for brevity app.UseExceptionHandler(“/Home/Error”); app.UseStatusCodePagesWithRedirects(“/Home/Error/{0}”); app.UseMvc(routes => { routes.MapRoute(“default”, “{controller=Home}/{action=Index}/{id?}”); routes.MapRoute(“spa-fallback”, “{*anything}”, new { controller = “Home”, action = “Index” }); routes.MapWebApiRoute(“defaultApi”, “api/{controller}/{id?}”); }); } 我希望回退,以便我的Angular2应用程序的路由不会导致HTTP状态代码为404,Not Found 。 但是,当用户无意中尝试导航到不存在的页面视图时,我还需要正确处理。 您可能会注意到我还调用了app.UseStatusCodePagesWithRedirects(“/Home/Error/{0}”); 。 使用状态代码和“spa-fallback”路由重定向到我的错误页面的调用似乎是互斥的 – 这意味着我似乎只能拥有一个或另一个(但遗憾的是不是两者) 。 有谁知道如何才能让两全其美?

MVC 4区域路由不起作用

我创建了一个空的MVC4应用程序,一切正常,之后我将一个区域添加到我的项目名为“主持人”。 我的区域路由代码是这样的: using System; using System.Web.Mvc; namespace EskimoArt.Areas.Moderator { public class ModeratorAreaRegistration : AreaRegistration { public override string AreaName { get { return “Moderator”; } } public override void RegisterArea(AreaRegistrationContext context) { context.MapRoute( “Moderator_default”, “Moderator/{controller}/{action}/{id}”, new {controller=”Dashboard”, action = “Index”, id = UrlParameter.Optional } ); } } } 我的Global.asx代码是这样的: using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; […]

MVC我的url正在创建“?Length = 4”

我正在创建一个MVC4应用程序。 我有一个小问题。 我的代码是 @Html.ActionLink(“Contract”, “Contract”, “Home”, new { id = “lnk_contract” }) 我正在收集url http://localhost:2355/Home/Contract?Length=4 我想要我的url http://localhost:2355/Home/Contract 我的结论是 routes.MapRoute( name: “Default”, url: “{controller}/{action}”, defaults: new { controller = “Home”, action = “Index”, id = UrlParameter.Optional } ); 如果你有答案,请帮帮我…

ASP.NET MVC Url.Action和路由名称值

我正在使用asp.net mvc 2并根据路线创建本地化。 我的路线如下: {culture}/{controller}/{action} 我去我的家庭控制器: en/Home/Index 我的家庭控制器视图有一个到其他控制器的链接: <a href='https://stackoverflow.com/questions/7938332/asp-net-mvc-url-action-and-route-name-value/’ >Products <a href='https://stackoverflow.com/questions/7938332/asp-net-mvc-url-action-and-route-name-value/’ >About 第一个链接生成代码: /en/Products/Prods但第二个生成: /Home/Index 当我在参数动作中传递值Index时,我无法理解为什么Url.Action跳过{culture} route参数? 我究竟做错了什么? 路线配置: routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”); routes.MapRoute(“Login”, // Route name “{controller}/Index”, // URL with parameters new { controller = “Login”, action = “Index” } // Parameter defaults ).RouteHandler = new SingleCultureMvcRouteHandler(); routes.MapRoute(“Default”, // Route name “{controller}/{action}/{id}”, // URL with […]

ASP.NET MVC URL生成性能

ASP.NET MVC的一个小基准。 浏览代码: public string Bechmark(Func url) { var s = new Stopwatch(); var n = 1000; s.Reset(); s.Start(); for (int i = 0; i < n; i++) { var u = url(); } s.Stop(); return s.ElapsedMilliseconds + " ms, " + ((s.ElapsedMilliseconds) / (float)n) + " ms per link”; } 查看代码: Url.Action(“Login”, “Account”)) %> […]