【问题标题】:MVC4 how to set default url?MVC4如何设置默认网址?
【发布时间】:2014-06-11 01:03:48
【问题描述】:

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

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

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

您可以通知这一点,但我在操作名称后添加了一个后缀,以便我可以调用控制器,显示类似“index.mon”的 URL

如果我在 URL 栏中手动将“index.mon”放在主机地址之后,那么它就可以了。 但是当应用程序自动启动时,它会抛出 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();
    }
}
}

(登录过程就别提了,我摘掉了几个代码。)

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

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

【问题讨论】:

  • 通过将 .mon 放在那里,您告诉路由仅在 url 的控制器部分包含 .mon 时路由。换句话说,你正在做与你想要的完全相反的事情。默认操作确实会转到特定的 url,它们是在没有给出 url 时执行的操作。
  • 感谢您的评论,那么我该如何设置您上面提到的默认操作?我希望我的应用程序开始使用“localhost/index.mon”怎么做?这就是我要问的:(
  • 你没有听我说的话。您正在尝试使默认操作执行他们无法执行的操作。默认操作仅显示“如果没有 url,则在此控制器上执行此操作方法”,即.. 如果您说 http://yoursite/(没有其他内容)。默认操作不会让您重定向到特定的 url。路由只解释当前 url 并基于它执行一个动作。
  • 默认操作不会产生这样的 url,我明白了。所以我去掉了路由设置中的默认选项。我仍然想知道的是,我必须设置什么样的设置才能使用“localhost/index.mon”运行我的应用程序......?需要修改 Web.config 什么的?
  • 我已经用谷歌搜索过了,但设置起始页只是我目前发现的。 :(

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


【解决方案1】:

我可以考虑只使用 hack 来解决这个问题。您可能想要创建一个虚拟操作,这将是默认操作,并且在被击中时会将请求重定向到您的 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 不工作(我稍后会探讨,也许它与静态文件有关),但上面的代码演示了该方法。

希望对您有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-18
    • 2017-02-13
    • 2017-10-12
    • 2012-03-04
    • 2011-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多