给定以下类和控制器操作方法:
public School
{
public Int32 ID { get; set; }
publig String Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string Street1 { get; set; }
public string City { get; set; }
public String ZipCode { get; set; }
public String State { get; set; }
public String Country { get; set; }
}
[Authorize(Roles = "SchoolEditor")]
[AcceptVerbs(HttpVerbs.Post)]
public SchoolResponse Edit(Int32 id, FormCollection form)
{
School school = …
Run Code Online (Sandbox Code Playgroud) 说我有以下路线:
routes.MapRoute("Default", "{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = "" });
Run Code Online (Sandbox Code Playgroud)
让我们说我的控制器有以下方法:Index(Int32 id)
和Edit(Int32 id)
.
所以,/MyController/Index/1
是该路线有效的URL.也是/MyController/Edit/1
但是,如果收到的URL正确映射到我的控制器而不是现有操作,那么如何定义要执行的"默认操作"而不是让MVC框架抛出错误屏幕?
基本上我想要URL /MyController/Preview/1
并/MyController/Whatever/1
执行我提前指定的操作,此时{action}令牌无法映射到我的控制器上的现有操作.
我看到Codeplex上的MvcContrib项目有一个属性,可以使它与ConventionController一起使用,但我现在想用纯MS ASP.NET MVC保留它.
我也看到Fredrik提到了一个[ControllerAction(DefaultAction = true)]
属性,但除了他的博客之外我无法在任何地方找到它(当我在我的控制器中尝试时,我的应用程序将无法编译).
无论路径信息如何,我都想阻止对任何.php或.cgi的请求.
例如,使用以下URL时:
http:// mysite /Admin/Scripts/Setup.php
它匹配现有路线:
routeCollection.MapRoute("Admin", "admin/{controller}/{action}/{uid}/{*pathInfo}", new { controller = "Admin", action = "Index", uid = "" });
Run Code Online (Sandbox Code Playgroud)
但是没有脚本控制器,所以MVC抛出以下内容:
IControllerFactory''没有返回名为'scripts'的控制器的控制器.
我真正喜欢的是,在MVC进入控制器之前,请求只是遇到了一次硬故障.
我知道我可以通过在Global.asax中挂钩Application_BeginRequest并抛出一个新的HttpException(404,"Not Found")来做到这一点,但这不是我想要的优雅解决方案.
我真的希望这会奏效:
routeCollection.IgnoreRoute("{resource}.php/{*pathInfo}");
Run Code Online (Sandbox Code Playgroud)
但事实并非如此.
注意:Sean Lynch的答案很有效,但我仍然非常喜欢基于System.Web.Routing或System.Web.Mvc的解决方案.这样我就可以允许我的用户在运行时添加自己的排除项.
我一直在研究Scott Guthrie关于ASP.NET MVC Beta 1的优秀帖子.在其中,他展示了对UpdateModel方法所做的改进以及它们如何改进单元测试.我已经重新创建了一个类似的项目,但是当我运行包含对UpdateModel的调用的UnitTest时,我会收到一个名为controllerContext参数的ArgumentNullException.
这是相关的位,从我的模型开始:
public class Country {
public Int32 ID { get; set; }
public String Name { get; set; }
public String Iso3166 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
控制器动作:
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(Int32 id, FormCollection form)
{
using ( ModelBindingDataContext db = new ModelBindingDataContext() ) {
Country country = db.Countries.Where(c => c.CountryID == id).SingleOrDefault();
try {
UpdateModel(country, form);
db.SubmitChanges();
return RedirectToAction("Index");
}
catch {
return View(country);
}
}
}
Run Code Online (Sandbox Code Playgroud)
最后我的单元测试失败了:
[TestMethod]
public void Edit() …
Run Code Online (Sandbox Code Playgroud) asp.net-mvc updatemodel argumentnullexception controllercontext
我正在尝试使用Dapper简单地将我的数据库表映射到C#中的类型,但是,我的一些类型需要不在表中的其他元素.为此,我使用的工厂可以获取列值并设置适当的属性.
public IEnumerable<IMyType> All() {
var query = _connection.Query("SELECT * FROM [table]");
return query.Select(o => _myTypeFactory.Create(o));
}
Run Code Online (Sandbox Code Playgroud)
目前这导致return语句生成错误:
无法将表达式转换'System.Collections.Generic.IEnumerable<dynamic>'
为返回类型'System.Collections.Generic.IEnumerable<IMyType>'
我的工厂类看起来像这样:
public class MyTypeFactory {
public IMyType Create(dynamic o) {
return Create((String) o.Code, (Int32) o.KeyID);
}
public IMyType Create(String code, Int32 keyID) {
return new MyType(code, Cache.Lookup(keyID));
}
}
Run Code Online (Sandbox Code Playgroud)
为什么Select()
方法不返回IEnumerable<IMyType>
?我需要做些什么来完成这项工作?这只是错误的做法,还有更好的方法吗?
我发现这个Python函数用于测试数字是否为素数; 但是,我无法弄清楚算法的工作原理.
def isprime(n):
"""Returns True if n is prime"""
if n == 2: return True
if n == 3: return True
if n % 2 == 0: return False
if n % 3 == 0: return False
i = 5
w = 2
while i * i <= n:
if n % i == 0:
return False
i += w
w = 6 - w
return True
Run Code Online (Sandbox Code Playgroud) asp.net-mvc ×3
algorithm ×1
c# ×1
dapper ×1
ignoreroute ×1
javascript ×1
jquery ×1
linq ×1
primes ×1
python ×1
updatemodel ×1