0

I don't know how best to describe this but here we go.

For the most part, Visual Studio is great at helping you navigate through code. Eg, if I see an unfamiliar line like this CRM.UpdateAdminAccounts(model.Email) I can hover over the object/method names to discover what they actually are, or right click and choose Go To Definition to jump straight to the code that makes the class, property, or whatever.

This is possible because the code is strongly typed and behind the scenes the compiler assigns symbols to everything making it easy for VS to know exactly what the text refers to and jump to it when required or find out where else it is referenced.

The same is not true of many lines of code in MVC where method names are referred to as a string literal. Eg return RedirectToAction("Index", "Home") or in a view: @Url.Action("Delete", new { id = item.ID })

If I want to jump straight to the Index or Delete action code I can't do it without lots of intermediate steps. Worse still, the action may not exist or there might be a typo not spotted until runtime.

Am I the only one to feel this is a huge step backward and could easily be improved with the use of reflection?

My question is simply, are there any tools or tricks I should be using to make this kind of thing possible and easier to use?

1
  • I believe VS doesn't support this natively. You might want to check Resharper. See this and this for info about navigation features. Commented Nov 14, 2022 at 14:34

1 Answer 1

5
+50

Your best bet is to have simple names for your methods that match the action names, and then redirect using nameof() in the string parameter.

public class CustomerController : Controller
{
    public ActionResult Details(int? id)
    {
        if (!id.HasValue)
            return RedirectToAction(nameof(Index));

        return View();
    }

    public ActionResult Index()
    {
        return View();
    }
}

Code sourced from ASP.NET MVC Controller Overview (C#) and modified slightly.

In this way, you can hover/click/follow reference on the Index part of nameof(Index) and VS will jump your cursor over to the Index() method.

This doesn't work perfectly if you are forced to disambiguate method names by making them more complex (e.g. if you had IndexOne() and IndexTwo()) for some reason.

The only other alternative, really, is to try and find a VS extension that can parse the code and jump you to a reference.

Sign up to request clarification or add additional context in comments.

2 Comments

I like that suggestion. It doesnt change anything logically but ensures the references are strongly typed and mistakes picked up at compile time. Why isn't this method more popular?
nameof is the correct answer to this one unless you're willing to install an extension to help you getting around. It not only gives you direct access to the reference, it's also a failsafe way to ensure all the references to that method are automatically updated if you refactor your action or method name (using Ctrl+R+R for instance).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.