Webapi和普通方法在同一个控制器中?

时间:2018-11-09 07:48:34

标签: c# asp.net-core

随着asp.net core 2.1中import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.FlowLayout; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; public class LayoutPractice implements ActionListener { //Set up variables private final JPanel graphic; private final JPanel under; private final JButton button; private final JLabel text; private int clicks; /** * Constructor, sets up GUI * */ public LayoutPractice() { //Default JFrame setup JFrame frame = new JFrame("Layout Practice"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the graphic panel graphic = new GraphicPanel(); graphic.setPreferredSize(new Dimension(500, 500)); //Set up the components that go under the graphic clicks = 0; button = new JButton("Click for dialog"); text = new JLabel("No Clicks"); //Set up the under panel, add in the button and label under = new JPanel(); under.setLayout(new FlowLayout()); under.add(button); under.add(text); //Tell it to react to the button being pressed button.addActionListener(this); JPanel mainPanel = new JPanel(new BorderLayout()); //Set the panels onto the JFrame mainPanel.add(graphic, BorderLayout.CENTER); mainPanel.add(under, BorderLayout.PAGE_END); frame.setContentPane(mainPanel); //Pack and set the JFrame to visible frame.pack(); frame.setVisible(true); } /** * Tells the GUI what to do when the button is pressed * * @param An ActionEvent, specifically the buton being pressed */ public void actionPerformed(final ActionEvent event) { //Increase the clicks variable clicks++; //Change/update the JLabel text.setText("Clicks: " + clicks); //Open a dialog using available tools JOptionPane.showMessageDialog(new JFrame(), ("Clicks: " + clicks), "Click Count", JOptionPane.INFORMATION_MESSAGE); } /** * Very simple main, makes a new LayoutPractice * * @param args */ public static void main(final String[] args) { new LayoutPractice(); } private class GraphicPanel extends JPanel { @Override public void paintComponent(final Graphics g) { super.paintComponent(g); //Set a color to pains g.setColor(Color.BLUE); //Use variables for a pattern int x = 0; int y = 0; //Loop for a pattern while (x <= 230) { //Generates a filled rectangle of the correct size g.fillRect(x, y, (500 - (2 * x)), (500 - (2 * y))); //Alternates color if (g.getColor() == Color.BLUE) { g.setColor(Color.ORANGE.darker()); } else { g.setColor(Color.BLUE); } //Increase variables to reduce rectangle size x += 20; y += 20; } } } } 属性的引入,我想知道如何使api和常规方法在同一控制器中工作。

$Number = (Get-Random 100) + 1
$Guess = 0
$count = 1

Write-Host "I'm thinking of a number between 1 and 100."

While ($Number -ne $Guess) {

Write-Host -NoNewline "What is the number? "
$Guess = [int] (Read-Host)

If ($Guess -gt $Number) { Write-Host "$Guess is too high." }
If ($Guess -lt $Number) { Write-Host "$Guess is too low." }   
If ($count -eq 7) {break}

}
Write-Host "Correct! $Number is the number I was thinking!"

我可以调用发布方法Apicontroller,但是不能运行[Route("api/[controller]")] [ApiController] public class OrderController : ControllerBase { [HttpPost] public async Task<IActionResult> SaveOrder(SaveOrderModel model) { //... } public async Task<IActionResult> CustomerOrders() { if (!User.IsInRole("Customer")) return Challenge(); var customer = await _workContext.CurrentCustomer(); var model = await orderModelFactory.PrepareCustomerOrderListModel(); return View(model); } }

  

它显示一个异常:InvalidOperationException:操作   '.CustomerOrders'没有属性路由。动作方法   用ApiControllerAttribute注释的控制器必须是attribute   已路由。

如果我在控制器级别上删除了[ApiController]和[Route(“ api / [controller]”))],而又将其放在了方法级别上,那么它肯定可以工作。仍然不知道这些方法是否有更好的混合解决方案,因为我想使用这项新的ApiController功能。

/api/order/saveorder

非常感谢任何输入。

1 个答案:

答案 0 :(得分:1)

您说的是,您无法呼叫https://example.com/order/customerorders。在您的[Route("api/[controller]")]中定义,该控制器内的所有方法将在https://example.com/api/order/可用。

因此,要调用您的方法,您需要调用https://example.com/api/order/customerorders

如果您想继续使用https://example.com/order/customerorders,则需要在方法中放置[Route]属性:

[ApiController]
public class OrderController : ControllerBase
{
        [HttpPost("api/order")]
        public async Task<IActionResult> SaveOrder(SaveOrderModel model)
        {
            ...

        }

        [HttpGet("order/customerorders")]
        public async Task<IActionResult> CustomerOrders()
        {
           if (!User.IsInRole("Customer"))
              return Challenge();
           var customer = await _workContext.CurrentCustomer();

           var model = await orderModelFactory.PrepareCustomerOrderListModel();
           return View(model);
        }
}