时间:2010-07-26 17:36:19

标签: asp.net-mvc

6 个答案:

答案 0 :(得分:45)

答案 1 :(得分:19)

要为2016/17/18更新此内容 - 最好的方法是使用属性路由。

在RouteConfig.cs中执行此操作的问题是旧路由仍然有效 - 因此您将同时使用

http://example.com/MyController/MyAction

http://example.com/MyAction

拥有多个到同一页面的路线对于搜索引擎优化是不利的 - 可能导致路径问题,并在整个应用程序中创建僵尸页面和错误。

使用属性路由可以避免这些问题,并且更容易看到哪些路由在哪里。您所要做的就是将其添加到RouteConfig.cs(可能在其他路线匹配之前位于顶部):

routes.MapMvcAttributeRoutes();

然后将路径属性添加到具有路径名称的每个操作,例如

[Route("MyAction")]
public ActionResult MyAction()
{
...
}

答案 2 :(得分:4)

以下是从HomeController中删除控制器名称的步骤

第1步: 创建路线约束。

public class RootRouteConstraint<T> : IRouteConstraint
{
    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var rootMethodNames = typeof(T).GetMethods().Select(x => x.Name.ToLower());
        return rootMethodNames.Contains(values["action"].ToString().ToLower());
    }
}

第2步:
在默认映射上方添加新路由映射,该映射使用我们刚创建的路由约束。泛型参数应该是您计划用作“根”控制器的控制器类。

routes.MapRoute(
    "Root",
    "{action}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    new { isMethodInHomeController = new RootRouteConstraint<HomeController>() }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

现在您应该能够访问您的家庭控制器方法,如下所示: example.com/about, example.com/contact

  

这只会影响HomeController。所有其他控制器将具有默认路由功能。

答案 3 :(得分:0)

答案 4 :(得分:0)

如果您希望将其应用于控制器(https://example.com/action)中的所有网址/操作,只需将控制器package com.emotion.slider.activities import android.os.Bundle import android.util.Log import android.view.MotionEvent import androidx.appcompat.app.AppCompatActivity import com.emotion.slider.R import com.github.mikephil.charting.charts.LineChart import com.github.mikephil.charting.components.XAxis import com.github.mikephil.charting.components.YAxis import com.github.mikephil.charting.data.Entry import com.github.mikephil.charting.data.LineData import com.github.mikephil.charting.data.LineDataSet import com.github.mikephil.charting.highlight.Highlight import com.github.mikephil.charting.interfaces.datasets.ILineDataSet import com.github.mikephil.charting.listener.ChartTouchListener import com.github.mikephil.charting.listener.OnChartGestureListener import com.github.mikephil.charting.listener.OnChartValueSelectedListener import com.github.mikephil.charting.utils.EntryXComparator import kotlinx.android.synthetic.main.mainactivity.* import java.util.* import kotlin.collections.ArrayList class MainActivity : AppCompatActivity(), OnChartGestureListener, OnChartValueSelectedListener{ private val TAG = "Main Activity" lateinit var chart: LineChart // LineChart is initialized From XML var keyFrames_TrackSlide = ArrayList<Entry>() override fun onChartGestureStart( me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture? ) { Log.i(TAG, "onChartGestureStart: X: " + me?.getX() + "Y: " + me?.getY()) } override fun onChartGestureEnd( me: MotionEvent?, lastPerformedGesture: ChartTouchListener.ChartGesture? ) { Log.i(TAG, "onChartGestureEnd: $lastPerformedGesture") } override fun onChartScale(me: MotionEvent?, scaleX: Float, scaleY: Float) { Log.i(TAG, "onChartScale: Scale-X: $scaleX Scale-Y: $scaleY") } override fun onChartLongPressed(me: MotionEvent?) { Log.i(TAG, "onChartLongPressed: ") } override fun onChartSingleTapped(me: MotionEvent?) { Log.i(TAG, "onChartSingleTapped: ") } override fun onChartDoubleTapped(me: MotionEvent?) { Log.i(TAG, "onChartDoubleTapped: " + me.toString()) // Convert Position Of Double Tap To A Point On The Graph var x: Float? = me?.x // Grab the X-Coordinate var y: Float? = me?.y // Grab the y-Coordinate var newTouchPoint = chart.getTransformer(YAxis.AxisDependency.LEFT).getValuesByTouchPoint(x!!, y!!) // Create a New Touch Point From The X and Y Coordinates (Will Map Raw Data To A Point On The Graph) var xValue = newTouchPoint.x var yValue = newTouchPoint.y var xValueRounded = "%.2f".format(xValue).toFloat() var yValueRounded = "%.2f".format(yValue).toFloat() var newEntry = Entry(xValueRounded, yValueRounded) Log.i(TAG, "ValueX: $xValueRounded, ValueY: $yValueRounded") // Grab Pre-Existing Data (Points) From Chart var chartDataPoints = chart.data if (chartDataPoints == null){ chartDataPoints = LineData() chart.data = chartDataPoints } keyFrames_TrackSlide.add( newEntry) Collections.sort(keyFrames_TrackSlide, EntryXComparator()) chart.data.clearValues() var set1 = LineDataSet(keyFrames_TrackSlide, "Data Set 1") set1.fillAlpha = 110 var dataSets = ArrayList<ILineDataSet>() dataSets.add(set1) var data = LineData(dataSets) chart.data = data chartDataPoints.notifyDataChanged() chart.notifyDataSetChanged() } override fun onChartFling( me1: MotionEvent?, me2: MotionEvent?, velocityX: Float, velocityY: Float ) { Log.i(TAG, "onChartDoubleFling: Velocity-X: $velocityX Velocity-Y: $velocityY") } override fun onChartTranslate(me: MotionEvent?, dX: Float, dY: Float) { Log.i(TAG, "onChartTranslate: dX: $dX dy: $dY") } override fun onNothingSelected() { Log.i(TAG, "onNothingSelected: ") } override fun onValueSelected(e: Entry?, h: Highlight?) { Log.i(TAG, "onValueSelected: " + e.toString()) } override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.mainactivity) createLineGraph() // Creates The Line Graph } private fun createLineGraph(){ // Setup LineChart From XML chart = this.lineChartFromXML // Grab LineChart from XML chart.isScaleYEnabled = false // Don't Allow User To Scale On Y-Axis chart.isScaleXEnabled = true // Allow User To Scale On X-Axis chart.legend.isEnabled = false // Disable The Chart's Legend (We'll provide our own) chart.maxHighlightDistance = 50.0f // Sets The max distance away a touch can be from a keyframe in order to access/modify the keyframe chart.isDoubleTapToZoomEnabled = false // Disable The Double Tap To Zoom Feature (We'll use double tap to signify a new keyframe) chart.onChartGestureListener = this@MainActivity // This Activity will be able to handle gestures from User input chart.setOnChartValueSelectedListener(this@MainActivity) // This activity will allow for the value selector listener to work // Setup Right Y-Axis' Settings val rightYAxis = chart.axisRight // Grab The LineChart's Right Y-Axis Object rightYAxis.setDrawGridLines(false) // Don't show grid lines on right side of Y-Axis rightYAxis.axisMaximum = 180F // Set The Maximum Of The Right Y-Axis to be 180 rightYAxis.axisMinimum = -180F // Set The Minimum Of The Right Y-Axis to be -180 rightYAxis.setLabelCount(5, true) // Show 5 Labels On The Right Y-Axis rightYAxis.textSize = 12.0f // Set Label Size // Setup Left Axis' Settings val leftYAxis = chart.axisLeft // Grab The LineChart's Left Y-Axis Object leftYAxis.setDrawGridLines(false) // Don't show grid lines on left side of Y-Axis leftYAxis.axisMaximum = 180F // Set The Maximum Of The Left Y-Axis to be 180 leftYAxis.axisMinimum = -180F // Set The Minimum Of The Left Y-Axis to be -180 leftYAxis.setDrawLabels(false) // Don't draw any labels on the Left Y-Axis // Setup X-Axis' Settings val xAxis = chart.xAxis // Grab The LineChart's X-Axis Object xAxis.axisMinimum = 0F // Set X-Axis' Minimum To 0 (seconds) xAxis.axisMaximum = 6F // Set X-Axis' Maximum to 6 (seconds) xAxis.position = XAxis.XAxisPosition.BOTTOM // Set The Labels of the X-Axis to be on the bottom of the X-Axis xAxis.textSize = 12.0f // Set Label Size // THE FOLLOWING IS PUTTING ENTRIES INTO AN ARRAY OF ENTRIES, WHICH THEN BECOMES AN ARRAY OF // DATA SETS, WHICH THEN BECOMES LINE DATA, WHICH THEN GETS ASSIGNED TO THE XML's LINE CHART, // FINALLY- THE VIEWER IS REFRESHED. // Step 1.) Create Entries With Position NOT Changing Over Time (Initial Chart's Keyframes) keyFrames_TrackSlide.add(Entry(0f, 0f)) // Time = 0 Sec, Position = Origin keyFrames_TrackSlide.add(Entry(6f, 0f)) // Time = 6 Sec, Position = Origin // Step 2.) Create a LineDataSet from the given Entries var dataSetTrackSlide = LineDataSet(keyFrames_TrackSlide, "TrackSlide") // Step 3.) Create an Array of the DataSets var dataSets = ArrayList<ILineDataSet>() dataSets.add(dataSetTrackSlide) // Step 4.) Create a LineData object from the DataSet Array var data = LineData(dataSets) // Step 5.) Assign The LineData to the chart from the XML chart.data = data // Step 6.) Refresh the XML view by calling invalidate() chart.invalidate() } } 设置为Route上方的空白即可。如果此控制器将成为您的启动控制器,则您还需要删除launchSettings.json中的每一行ApiController

launchUrl

答案 5 :(得分:-3)

routes.MapRoute("SpecificRoute", "MyController/{action}/{id}", 
         new {controller = "MyController", action = "Index", 
         id = UrlParameter.Optional});

// default route
routes.MapRoute("Default", "{controller}/{action}/{id}", 
         new {controller = "Home", action = "Index", 
         id = UrlParameter.Optional} );