Rei*_*l-- 2 asp.net-mvc intellisense visual-studio-2015
Visual Studio的Intellisense知道哪些View文件可用.
如果我写这个(错误的故意):
[HttpGet]
public ActionResult Create()
{
return View("Craete", new KeywordViewModel());
}
Run Code Online (Sandbox Code Playgroud)
然后它会发出呜呜声,而不是强调它并说"无法解析视图".这很可爱.
如果我想添加一个间接层,并通过另一个函数传递"Create":
[HttpGet]
public ActionResult Create()
{
return ArbitraryIndirection("Craete");
}
public ActionResult ArbitraryIndirection(string viewName)
{
return View(viewName, new KeywordViewModel());
}
Run Code Online (Sandbox Code Playgroud)
然后我失去了这种行为.智能感知不知道该ArbitraryIndirection期待一个视图的目标,所以它不检查.这并不奇怪,但令人难过.
我可以教Intellisense更聪明吗?
是否有一些东西告诉Intellisence这个参数是特殊的(我猜一个XML注释引用,或者一个属性,也许?)或者它是否在某个地方被魔术硬编码到Intellisence?
我可以在间接View参考上获得写入时错误检查吗?
我在大约一年内没有这样做过,但当时正是ReSharper巧妙地验证了我的观点.
如果您使用的是ReSharper,则可以使用程序[AspMvcView]集中的属性JetBrains.Annotations- 您可以通过Nuget包含该属性.相关指南出现在jetbrains网站的此链接上:
以下是[AspMvcAction]和[AspMvcController]属性的示例用法(请注意,您需要使用该[AspMvcView]属性,但我没有使用该代码的代码)
public static MvcHtmlString WidgetWrapperAction(this HtmlHelper<dynamic> html, [AspMvcAction] string action, [AspMvcController] string controller, object parameters)
{
var routeValueDictionary = new RouteValueDictionary(parameters) { { "area", "AreaName" } };
var htmlString = html.Action(action, controller, routeValueDictionary);
if (string.IsNullOrWhiteSpace(htmlString.ToHtmlString()))
{
htmlString = html.Action("WidgetLoadFailed", "WidgetLoadFailed", new RouteValueDictionary { { "area", "AreaName" } });
}
return htmlString;
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助.
| 归档时间: |
|
| 查看次数: |
105 次 |
| 最近记录: |