如何检查MVC中的路由?

时间:2014-03-02 19:24:21

标签: asp.net-mvc asp.net-mvc-3

好的,所以我很想知道如何检查我的MVC应用程序中当前的巫婆路线,因为目前我有MVC3应用程序的这个设置,据我所知一切都是正确的,但“数据”路线仍然不起作用:

的global.asax

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using InteractiveAnalysis.Common;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Diagnostics;

using InteractiveAnalysis.App_Start;
using System.Web.Optimization;

namespace InteractiveAnalysis
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : ToolsFramework.Mvc.ToolsFrameworkHttpApplicationBase
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Excel", // Route name
                "excel", // URL with parameters
                new { controller = "Excel", action = "Index"} // Parameter defaults
            );

            routes.MapRoute(
                "Data", // Route name
                "data", // URL with parameters
                new { controller = "Data", action = "Index" } // Parameter defaults
            );

            routes.MapRoute(
                "Version", // Route name
                "version/{action}", // URL with parameters
                new { controller = "Version", action = "Index" } // Parameter defaults
            );
            routes.MapRoute(
                "Main", // Route name
                "{ver}", // URL with parameters
                new { controller = "Main", action = "Index", ver = UrlParameter.Optional } // Parameter defaults
            );




            /*
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("cache/{action}/{id}");

            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Main", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            */
        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);

            UnityObject.Register();
            ToolsFramework.Cache.OutputCacheHandeler.addCacheKey(UnityObject.APPLICATION_NAME);

            //OldBundleConfig.RegisterBundles(BundleTable.Bundles);
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Response.Clear();
            Exception exception = Server.GetLastError();
            HttpException httpException = (exception.GetBaseException() as HttpException);

            if (httpException != null)
            {
                switch (httpException.GetHttpCode())
                {
                    case 501: //function not implemented
                        Server.ClearError();
                        Response.Write(exception.Message);
                        return;
                }
            }


#if !DEBUG 
            if (Euroland.Azure.Utilities.AzureEnvironment.IsAvailable)
            {
                EventLog.WriteEntry(InteractiveAnalysis.Common.UnityObject.APPLICATION_NAME, "Error:\r\n\r\n" + exception.Message + "\r\n\r\nStack Trace:\r\n" + exception.StackTrace, EventLogEntryType.Error);
            }
            else 
            {
                Exception exx = Server.GetLastError().GetBaseException();
                System.Diagnostics.StackTrace trace = new System.Diagnostics.StackTrace(exx, true);

                string Category = "ASP.NET error";
                string ASPCode = "N/A";
                string ASPDescription = exx.ToString();
                if (ASPDescription.Length > 500) { ASPDescription = ASPDescription.Substring(0, 500); }
                string Column = trace.GetFrame(0).GetFileColumnNumber().ToString();
                string Description = exx.Message.ToString();
                if (Description.Length > 500) { Description = Description.Substring(0, 500); }
                string File = trace.GetFrame(0).GetFileName();
                string Line = trace.GetFrame(0).GetFileLineNumber().ToString();
                string Number = "N/A";
                string Source = trace.GetFrame(0).GetMethod().Name;
                if (Source.Length > 500) { Source = Source.Substring(0, 250); }
                string ServerName = HttpContext.Current.Server.MachineName;  //Request.ServerVariables["SERVER_NAME"];
                string ServerIP = Request.ServerVariables["LOCAL_ADDR"];
                string RemoteIP = Request.ServerVariables["REMOTE_ADDR"];
                string UserAgent = Request.ServerVariables["HTTP_USER_AGENT"];
                string Referer = Request.ServerVariables["REFERER"];
                string URL = Request.Url.ToString();

                //currently it can function in the local enviroment
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocaleErrorConnectionString"].ToString());

                SqlCommand myCommand = new SqlCommand();

                myCommand.CommandType = CommandType.StoredProcedure;
                myCommand.CommandText = "spInsertServerError";
                myCommand.Connection = conn;
                myCommand.Parameters.AddWithValue("@Category", Category);
                myCommand.Parameters.AddWithValue("@ASPCode", ASPCode);
                myCommand.Parameters.AddWithValue("@ASPDescription", ASPDescription);
                myCommand.Parameters.AddWithValue("@Column", Column);
                myCommand.Parameters.AddWithValue("@Description", Description);
                myCommand.Parameters.AddWithValue("@File", File);
                myCommand.Parameters.AddWithValue("@Line", Line);
                myCommand.Parameters.AddWithValue("@Number", Number);
                myCommand.Parameters.AddWithValue("@Source", Source);
                myCommand.Parameters.AddWithValue("@ServerName", ServerName);
                myCommand.Parameters.AddWithValue("@ServerIP", ServerIP);
                myCommand.Parameters.AddWithValue("@RemoteIP", RemoteIP);
                myCommand.Parameters.AddWithValue("@UserAgent", UserAgent);
                myCommand.Parameters.AddWithValue("@Referer", Referer);
                myCommand.Parameters.AddWithValue("@URL", URL);

                try
                {
                    conn.Open();
                    myCommand.ExecuteNonQuery();
                }
                catch { }
                finally
                {
                    if (conn.State != ConnectionState.Closed) { conn.Close(); }
                    //Server.ClearError();
                }
            }
#endif
        }
    }
}

DataController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Text;
using System.Globalization;
using InteractiveAnalysis.Models;
using ToolsFramework;

namespace InteractiveAnalysis.Controllers
{
    public class DataController : Controller
    {
        //
        // GET: /Data/


        public string Index()
        {
            return "something";

        }

    }
}

但是每次我检查“localhost:62570 / data /”我都会得到404而我却得不到它。我错过了为什么没有采用“数据”路线?据我所知,我已经做好了一切。

1 个答案:

答案 0 :(得分:1)

检查/调试路由的最佳和最简单的方法是使用Phil Haack的路由调试器,您可以使用以下nuget package安装它