简体   繁体   中英

How to get syntax highlighting of controllers and actions on HtmlHelper extension method?

How can I get Visual Studio to provide controller and action syntax highlighting like it offers for its own object for controller and action strings in the following?

When you use:

Html.Action("Index", "Home");

Visual Studio provides additional highlighting on the controller and action, it even tries to detect which ones are available and will present it as an option.

Notice in the image below how it give me options for the actions based upon the controller that I've entered. This is what I would like to happen for my custom code. 控制器/动作善良

I have perfectly working HtmlHelper extension method similar to the following:

public static MvcHtmlString Action(this HtmlHelper htmlHelper, string actionName, string controllerName, string myAwesomeField) {
        return Action(htmlHelper, actionName, controllerName, null /* routeValues */, myAwesomeField);
    }

My extension methods are getting picked up by intellisense in the regard that it knows I need a few strings. When you look at Microsoft's code , the parameters for action and controller are strings, but Visual Studio knows whether those controllers and actions exists and provide additional intellisense support. How does the tooling pick up on it?

If you are using Resharper (of course you are) then here's a perfect solution that I had working in 5 minutes:

Follow the instruction here at the Jetbrains blog . I elected for option 2 of having the annotations source code in a file named jetbrains.Annnotations.cs included in my project. Then simply by adding the parameter attribute decorations as below (in my method WithLink ) everything just works .

public void WithLink([AspMvcAction] string action, [AspMvcController] string controller)

Nice

您必须将扩展程序所在的名称空间包含在Views文件夹的web.config或特定视图本身中(通过@using ),以获得智能支持。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM