我们正在考虑推进ASP.NET MVC项目,并提出了路由与参数的主题.
看看如何在ASP.NET MVC中轻松设置其中任何一个或两者的组合,在使用其中一个时,我应该注意哪些注意事项?
使用ASP.NET MVC(3或4DP)中的开箱即用方法定位器,有没有办法让MVC框架区分字符串和Guid而无需解析控制器操作中的参数?
用法示例适用于URL
执行
public ActionResult Details(Guid guid)
Run Code Online (Sandbox Code Playgroud)
方法,和
执行
public ActionResult Details(string id)
Run Code Online (Sandbox Code Playgroud)
方法.
没有任何变化,显然这些方法含糊不清,如下:
public ActionResult Details(Guid id)
{
var model = Context.GetData(id);
return View(model);
}
public ActionResult Details(string id)
{
var model = Context.GetData(id);
return View(model);
}
Run Code Online (Sandbox Code Playgroud)
导致错误:
The current request for action 'Details' on controller type 'DataController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Details(System.Guid) on type Example.Web.Controllers.DataController
System.Web.Mvc.ActionResult Details(System.String) on type Example.Web.Controllers.DataController
Run Code Online (Sandbox Code Playgroud)
我尝试使用自定义约束(基于如何创建System.Guid类型的路由约束?)尝试通过路由推送它:
routes.MapRoute(
"Guid", …Run Code Online (Sandbox Code Playgroud) 我很好〜/映射到Home Index,并且〜/ Blog映射到Blog Index,但是如何防止〜/ Home映射到Home Index呢?我不希望从多个端点访问路由.
同样,如何防止〜/ Controller和〜/ Controller/Index中的所有其他"索引"操作都可以访问?
OK〜/
NO~/Home
NO~/Home/Index
OK~/AnyOtherController
NO~/AnyOtherController/Index
我想该规则应该类似于阻止任何默认操作可以显式访问,并且在home的情况下也可以防止仅使用控制器访问它.
可以这样做吗?这是在过去完成的吗?例如,SO不会这样做(你可以在这里或那里访问)并且都渲染主页; 并且它们可能具有与"index"不同的默认操作名称,这可能也是可访问的路径.
我在使用MVC MiniProfiler时看到一个非常疯狂的错误.间歇性地,我正在处理的网站进入一个状态,每个请求都会导致抛出此异常:
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
---> System.TypeInitializationException: The type initializer for 'MvcMiniProfiler.MiniProfiler' threw an exception.
---> System.Threading.LockRecursionException: Write lock may not be acquired with read lock held.
This pattern is prone to deadlocks. Please ensure that read locks are released before taking a write lock.
If an upgrade is necessary, use an upgrade lock in place of the read lock.
at System.Threading.ReaderWriterLockSlim.TryEnterWriteLockCore(Int32 millisecondsTimeout)
at System.Threading.ReaderWriterLockSlim.TryEnterWriteLock(Int32 millisecondsTimeout)
at System.Web.Routing.RouteCollection.GetWriteLock()
at MvcMiniProfiler.UI.MiniProfilerHandler.RegisterRoutes()
in …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我最近从ASP.NET MVC1升级到ASP.NET MVC4 rc1.
它使用Webforms视图引擎.
每当使用Url.Action(动作,控制器)时,它都会出现性能问题.
我可以在ASP.NET MVC3中重现该问题.
我需要3ms来渲染在ASP.NET MVC1中有10个Url.Action帮助器实例的视图,在ASP.NET MVC3中渲染相同的40ms.
我已经找到了一些让它渲染得更快的方法:
我将默认路线移到了顶部
我删除了Url.Action并使用了静态链接
这感觉不对:应用程序非常大,我需要一个体面的工作路由的好处.我也不相信我发现了所有的性能瓶颈.路由是MVC的核心部分:如果某些事情表现不佳,它将弹出应用程序的不同部分.
我的印象是MVC3引入了一些路由功能(如正则表达式约束),即使我不使用它们也会导致性能不佳的应用程序.
有什么我可以做的事情,如转换路由功能或使用一组不同的URL助手?
此代码重现了此问题:
指数行动
public ActionResult Index()
{
return View();
}
Run Code Online (Sandbox Code Playgroud)
的Index.aspx
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head >
<title></title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="page">
<%= Url.Action("Action1", "Controller1") %>
<%= Url.Action("Action2", "Controller2") %>
<%= Url.Action("Action3", "Controller3") %>
<%= Url.Action("Action4", "Controller4") %>
<%= Url.Action("Action5", "Controller5") %>
<%= Url.Action("Action6", "Controller6") %>
<%= Url.Action("Action7", …Run Code Online (Sandbox Code Playgroud) asp.net-3.5 asp.net-mvc-routing asp.net-4.0 asp.net-mvc-3 asp.net-mvc-2
我们在将WebAPI服务部署到生产服务器上时遇到问题.尝试访问Controller时,我们收到"您要查找的资源已被删除,其名称已更改或暂时不可用".
我们从标准的WebAPI模板创建了一个新项目,它有同样的问题,因此绝对不是我们的代码导致了这个问题.我们还有另一台服务器与相同的托管公司,并在部署到一切正常.服务器之间的所有IIS功能都相同.
在一个阶段,我们将应用程序池设置为.Net Framework 2.0,并且网站没有抱怨,因此它甚至没有尝试启动该服务.我们在同一个Web应用程序上也有odata服务,它运行正常.所以它似乎与MVC路由和WebAPI有关.
我们将我们能想到的每个dll复制到服务器,但没有解决问题.
有没有人对可能导致上述错误的2台服务器之间有什么不同有任何想法?
提前致谢
我的应用程序中使用了以下类型的URL.
本地主机/管理/ userdetail/ID
本地主机/管理/ userdetail/ID /真
本地主机/管理/ userdetail/ID /真/成功
这是我的管理员控制器
bool inSaveAction,字符串状态是可选的
[Authorize]
public ActionResult UserDetail(string Id, bool inSaveAction, string status)
{
}
[HttpPost, Authorize, ValidateAntiForgeryToken]
public ActionResult SaveUserDetail(UserDetailViewModel viewModel)
{
User userToSave = new User();
AdminService.UpdateUser(userToSave);
//This is calling the above function as it sending all 3 params
return RedirectToAction("UserDetail", new { Id = viewModel.Id,
inSaveAction = true, status = "success" });
}
Run Code Online (Sandbox Code Playgroud)
以下情况不起作用
@Html.ActionLink("DisplayName", "UserDetail", new { id = Model.Id })
Run Code Online (Sandbox Code Playgroud)
在Global.asax中
routes.MapRoute("UserDetail",
"UserDetail/{id}",
new
{
controller = …Run Code Online (Sandbox Code Playgroud) 我的Razor Layout视图中有以下代码(由我的应用程序中的所有视图共享):
@using (Html.BeginForm("Logout", "Account", FormMethod.Post, new { id = ViewIDs.Shared._AuthenticationPartial.LogoutForm })) {
Run Code Online (Sandbox Code Playgroud)
这与我的家庭和帐户视图一起工作正常,即它呈现了一个发布到〜/ Account/Logout的表单.但是,当与名为"Person"的区域内的视图一起使用时,它会突然发布到〜/ Person/Account/Logout.
现在,我能够解决这个问题如下:
@using (Html.BeginForm("Logout", "Account", new { area = "" }, FormMethod.Post, new { id = ViewIDs.Shared._AuthenticationPartial.LogoutForm })) {
Run Code Online (Sandbox Code Playgroud)
这是否是正确的方法,即根据定义当前区域的默认区域?或者我的应用程序中存在配置问题?
我在ASP.NET MVC 5应用程序中有一个服务类,它应该生成一个完全限定的路由URL.
从控制器调用此类.
在控制器中,我通常生成路由的URL,如:
string myUrl = Url.RouteUrl("ViewCust", new{custId=customerId},Request.Url.Scheme)
Run Code Online (Sandbox Code Playgroud)
如何在不是控制器的类中生成路由URL?
我试过了:
string myUrl = System.Web.Mvc.UrlHelper()
.RouteUrl(
routeName,
routeValues,
HttpContext.Current.Request.Url.Scheme);
Run Code Online (Sandbox Code Playgroud)
这导致了
'System.ArgumentNullException'值不能为null.参数名称:routeCollection
我正在尝试建立自己的小cms.我创建了一个抽象的pageBase类,它由Static,Reviews,Articles,News继承.每个都有自己的控制器方法.
我的问题是我需要允许管理员定义自己的自定义路径级别.例如news\local\mynewdog或Articles\events\conventions\mycon.所以我想要一种传递字符串数组并设置自定义路由的方法.
c# asp.net-mvc url-routing asp.net-mvc-routing custom-routes
asp.net-mvc ×8
url-routing ×2
asp.net ×1
asp.net-3.5 ×1
asp.net-4.0 ×1
c# ×1
guid ×1
iis ×1
seo ×1