【问题标题】:Redirect my website to a default controller in mvc将我的网站重定向到 mvc 中的默认控制器
【发布时间】:2015-02-21 09:13:08
【问题描述】:

我的项目中有一个名为Fa 的区域在我的fa 区域中我有一个名为home 的控制器,在我的控制器内部我有一个名为index 的操作。

我发布了我的网站,但是当我输入我的网址时,我看不到我的网站,因为它应该被重定向到mywebsite.com/en/home/index。如何在我的MVC Project 中设置此网址

我试过这个,但它不起作用。

var route = routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    ).DataTokens = new RouteValueDictionary(new { area = "fa" });

【问题讨论】:

  • 是什么让你认为它是mywebsite.com/en/home/index 从你给它的路线应该是mywebsite.com/fa/home/index
  • 你可以看到我的网站:www.exportvision.org ,但是当你输入它时它不起作用,但是使用这个网址它可以工作 www.exportvision.org/en/home/index跨度>

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


【解决方案1】:

你应该使用这个代码:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
                routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new {area="fa", controller = "Home", action = "Index", id = UrlParameter.Optional },namespaces: new string[] { "UI.Areas.fa.Controllers"}
            ).DataTokens.Add("area", "fa");

【讨论】:

    【解决方案2】:

    您需要为默认路由指定命名空间。由于您有多个 HomeController,它需要知道要渲染哪一个。

    根据您的 cmets,您还需要修改默认值。

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { area = "en", controller = "Home", action = "Index", id = UrlParameter.Optional },
        namespaces: new string[] { "UI.Areas.en.Controllers" }
    );
    

    【讨论】:

    • 注意根据你的错误信息。 This can happen if the route that services this request ('{controller}/{action}/{id}') does not specify namespaces to search for a controller that matches the request. If this is the case, register this route by calling an overload of the 'MapRoute' method that takes a 'namespaces' parameter.
    • 是的,你是对的,我更改了我的代码并在本地机器上运行它让我上传它,因为错误已更改
    • 我上传了 bin 文件夹,请检查 URL:www.exportvision.org,错误是:未找到视图“索引”或其母版,或者没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Home/Index.aspx ~/Views/Home/Index.ascx ~/Views/Shared/Index.aspx ~/Views/Shared/Index.ascx ~/Views/Home/Index。 cshtml ~/Views/Home/Index.vbhtml ~/Views/Shared/Index.cshtml ~/Views/Shared/Index.vbhtml
    • @EA 这是一个单独的错误。默认文件夹结构中没有视图。您的视图应位于~/Views/Home/Index.cshtml
    • 我在这个文件夹中没有任何视图,我的项目中有一个区域,它应该是 area/en/view/home/index
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-11-30
    • 1970-01-01
    • 2020-03-13
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多