这里我是mvc core2.0的新用户,请帮我解释为什么我的路由不能正常工作 我的路由类
/*
* Class MyColorChooser holds sliders affecting text fields, labels, and a
* graphic affected by the position of the sliders.
*/
package creatingacolorchooser;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.Graphics;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
/**
*
* @author ian20
*/
public class MyColorChooser extends JPanel
{
private int red; //Holds int value as red.
private int green; //Holds int value as green.
private int blue; //Holds int value as blue.
private JLabel redLabel; //Holds red color label
private JLabel greenLabel; //Holds blue color label
private JLabel blueLabel; //Holds green color label
private JTextField redText; //Holds numerical value of red colod
private JTextField greenText; //Holds numerical value of green colod
private JTextField blueText; //Holds numerical value of blue colod
private JSlider redSlider; //Holds the red JSlider
private JSlider greenSlider; //Holds the green JSlider
private JSlider blueSlider; //Holds the blue JSlider
public MyColorChooser()
{
//Create layout
setLayout(new FlowLayout());
//Create labels
redLabel = new JLabel("Red: ");
greenLabel = new JLabel("Green: ");
blueLabel = new JLabel("Blue: ");
//Create read only text fields.
redText = new JTextField("0", 5);
redText.setEditable(false);
greenText = new JTextField("0", 5);
greenText.setEditable(false);
blueText = new JTextField("0", 5);
blueText.setEditable(false);
//Create JSlider
redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
redSlider.setPreferredSize(new Dimension(1000,75));
redSlider.setMajorTickSpacing(10);
redSlider.setMinorTickSpacing(1);
redSlider.setPaintTicks(true);
redSlider.setPaintLabels(true);
redSlider.addChangeListener(new ColorSlideListener());
greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
greenSlider.setPreferredSize(new Dimension(1000,75));
greenSlider.setMajorTickSpacing(10);
greenSlider.setMinorTickSpacing(1);
greenSlider.setPaintTicks(true);
greenSlider.setPaintLabels(true);
greenSlider.addChangeListener(new ColorSlideListener());
blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
blueSlider.setPreferredSize(new Dimension(1000,75));
blueSlider.setMajorTickSpacing(10);
blueSlider.setMinorTickSpacing(1);
blueSlider.setPaintTicks(true);
blueSlider.setPaintLabels(true);
blueSlider.addChangeListener(new ColorSlideListener());
//Add components to the panel
add(redLabel);
add(redSlider);
add(redText);
add(greenLabel);
add(greenSlider);
add(greenText);
add(blueLabel);
add(blueSlider);
add(blueText);
}
/**
* Private inner class to handle the the change events that are generated
* when the slider is moved
*/
private class ColorSlideListener implements ChangeListener
{
public void stateChanged(ChangeEvent e)
{
//get slider value
red = redSlider.getValue();
green = greenSlider.getValue();
blue = blueSlider.getValue();
//Display slider value in text field.
redText.setText(Integer.toString(red));
greenText.setText(Integer.toString(green));
blueText.setText(Integer.toString(blue));
//Set rectangle color.
repaint();
}
}
/**
* The paintComponent creates a rectangle with adjustable coloring
* @param g
*/
@Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.setColor(new Color(red, green, blue));
g.fillRect(420, 300, 300, 100);
}
}
/*
* The MainColor creates the GUI for the Selected RectPanel application.
*/
package creatingacolorchooser;
import java.awt.GridLayout;
import javax.swing.JFrame;
/**
*
* @author ian20
*/
public class MainColor
{
public static void main(String[] args)
{
//Create frame object
JFrame frame = new JFrame("Change Rectangle Color");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create rect object
MyColorChooser mcc = new MyColorChooser();
//Add to the frame, set layout, set size, and make it viable.
frame.setLayout(new GridLayout(1, 1));
frame.add(mcc);
frame.setSize(1150, 500);
frame.setVisible(true);
}
}
此类文件我在startup.config文件中注册
public static class ApplicationRoteProfiler
{
public static void Routeing(IRouteBuilder builder)
{
builder.MapRoute("route1", "", new
{
Controllers = "Department",
Action = "Add",
});
builder.MapRoute("route2", "Department/Add", new
{
Controllers = "Department",
Action = "Index"
});
}
当我点击我的服务器为 public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles();
app.UseMvc();
app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
ApplicationRoteProfiler.Routeing(routes);
});
}
时,它应该重定向到部门/索引但它点击部门/添加
答案 0 :(得分:2)
应该只是Controller
而不是Controllers
??
builder.MapRoute("route1", "", new { controller = "department", action = "index" });
app.UseMvcWithDefaultRoute()
和app.UseMvc()
。你只需要选择其中的一个。我没有看到使用静态类为MVC配置路由的好处。您可以将所有路径配置放在UseMvc
lamba函数中。此外,我认为您不需要专门为您的" route1"因为它遵循标准的MVC路由约定。
app.UseMvc(routes =>
{
// The order of these routes matters!
routes.MapRoute(
name: "route2",
template: "department/add",
defaults: new { area = "", controller = "department", action = "index" });
routes.MapRoute(
name: "default",
template: "{controller=home}/{action=index}/{id?}");
}
您还可以在return RedirectToAction("index");
控制器Department
方法中Add
,因此每当/deparment/add
路由被点击时,它会重定向到/deparment/index
,假设您拥有默认的MVC路由设置,使用"默认"路线我穿上#2,或使用UseMvcWithDefaultRoute()
。这样您就不需要为重定向创建自定义路由。
public class DepartmentController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult Add()
{
return RedirectToAction("index");
}
}