在控制器MVC中自动生成动作。

时间:2022-12-01 23:34:44

I'm trying to use VS more efficiently, and I was looking for a way to generate a method automatically. For example, I know if you type foreach then press TAB twice it generates the skeleton code, so if I had a method like this:

我试图更有效地使用VS,我正在寻找一种自动生成方法的方法。例如,我知道如果你输入foreach然后按TAB两次它会生成骨架代码,所以如果我有这样的方法:

[HttpPost]
public ActionResult CloseTicket()
{
    //do stuff
}

Is there a way of generating the 'skeleton code' so I don't have to manually type it out. I looked into method stubs but they don't seem to apply to this.

有没有一种生成“骨架代码”的方法,这样我就不用手工输入了。我研究了方法存根,但它们似乎并不适用于此。

2 个解决方案

#1


23  

There are built-in snippets in Visual Studio to help with this:

在Visual Studio中有一些内置的代码片段来帮助实现这一点:

mvcaction4

inserts:

插入:

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

And

mvcpostaction4

Inserts

插入

[HttpPost]
public ActionResult Action()
{
    return View();
}

#2


2  

If it is not there you can create a code snippet. Just put in in the right folder and VS will use it.

如果没有,您可以创建一个代码片段。只要把它放在正确的文件夹里,VS就会使用它。

For VS2013 put it in folder:

VS2013将其放入文件夹:

\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets

\Visual Studio 2013\代码片段\Visual c# \我的代码片段

See here how to create a code snippet https://msdn.microsoft.com/en-us/library/ms165394.aspx

参见这里如何创建代码片段https://msdn.microsoft.com/en-us/library/ms165394.aspx

#1


23  

There are built-in snippets in Visual Studio to help with this:

在Visual Studio中有一些内置的代码片段来帮助实现这一点:

mvcaction4

inserts:

插入:

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

And

mvcpostaction4

Inserts

插入

[HttpPost]
public ActionResult Action()
{
    return View();
}

#2


2  

If it is not there you can create a code snippet. Just put in in the right folder and VS will use it.

如果没有,您可以创建一个代码片段。只要把它放在正确的文件夹里,VS就会使用它。

For VS2013 put it in folder:

VS2013将其放入文件夹:

\Documents\Visual Studio 2013\Code Snippets\Visual C#\My Code Snippets

\Visual Studio 2013\代码片段\Visual c# \我的代码片段

See here how to create a code snippet https://msdn.microsoft.com/en-us/library/ms165394.aspx

参见这里如何创建代码片段https://msdn.microsoft.com/en-us/library/ms165394.aspx