MVC4如何设置默认网址?

时间:2014-06-11 01:03:48

标签: asp.net-mvc-4 web-config asp.net-mvc-routing

我正在构建一个MVC4 Web应用程序,并希望在我的Web应用程序的起点设置我想要使用的特定URL。

所以我改变了RouteConfig.cs中的一些值,如下所示。

   routes.MapRoute(
          name: "Default",
          url: "{action}.mon/{id}",
          defaults: new { controller = "login", action = "index", id = UrlParameter.Optional }
   );

你可以通知这个,但是我在动作名后添加了一个后缀,这样我就可以调用一个控制器,显示像“index.mon”这样的URL

如果我手动将“index.mon”放在URL栏中的主机地址之后,那么它的工作正常。 但是当应用程序自动启动时,会抛出403.14错误。 (“自动启动”在这里意味着我通过使用F5键运行此应用程序来运行临时IIS服务器。)

登录控制器看起来像这样

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.OleDb;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Monarch815MVC.Controllers
{
public class loginController : Controller
{
    //
    // GET: /login/

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

    public ActionResult loginProcess(string id = "000000", string pass = "example", string scode = "co")
    {
        Dictionary<string, object> sessionData = null;

        String SqlCommand = "USP_LOGIN";

        DataSet UserInfo = dataController.ExecuteDataset(dataController.CONN_STRING, CommandType.StoredProcedure, SqlCommand, arParms);

        if (UserInfo.Tables[0].Rows.Count > 0)
        {
            sessionData = new Dictionary<string, object>();
            for (int i = 0; UserInfo.Tables[0].Rows[0].Table.Columns.Count > i; i++)
            {
                sessionData.Add(UserInfo.Tables[0].Rows[0].Table.Columns[i].Caption, UserInfo.Tables[0].Rows[0].ItemArray[i]);
            }
        }

        return View();
    }
}
}

(让我们忘掉loginProcess,我取了几个代码。)

我是否必须在index()或Web.config中返回阶段做些什么?或者,RouteConfig.cs?

我必须使用后缀“.mon”来调用控制器。

1 个答案:

答案 0 :(得分:0)

我可以想到只使用黑客来解决这个问题。您可能想要创建一个虚拟操作,这将是默认操作,并且在被命中时会将请求重定向到您的index.mon

我试过以下,请检查这是否对您有所帮助。感谢。

路线配置

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

            routes.MapRoute(
                name: "DefaultY",
                url: "{action}-mon/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

            routes.MapRoute(
              name: "DefaultX2",
              url: "{action}/{id}",
              defaults: new { controller = "Home", action = "Startup", id = UrlParameter.Optional }
          );
        }

控制器

public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Startup()
        {
            return RedirectToRoute("DefaultY", new {controller = "Home", action = "Index" });
        } 
    }

对我来说,我使用了index-mon。 index.mon无法正常工作(稍后我会探讨,也许它与静态文件有关),但上面的代码演示了这种方法。

希望有所帮助。

相关问题