来自uri的ASP MVC Rounting,Remove / Areas / Prefix

时间:2014-08-04 19:17:28

标签: asp.net-mvc single-page-application asp.net-mvc-areas

默认区域有/ Area / routing前缀。例如,博客区域将是:

/Areas/Blog/Blog/Show/myId

/Areas/{area}/{controller}/{action}/{id}

结果是Web应用程序的一个非常丑陋和冗长的uri。我想要的是:

/Blog/Show/myId

/{area / controller}/{action}/{id}

我怎样才能做到这一点?

我使用区域的原因是因为我的博客' area不是标准的MVC应用程序,而是单页面应用程序。它具有与应用程序其余部分不同的项目结构,因此,我想将其划分为自己的区域。

编辑:区域注册来源

public class BlogAreaRegistration : AreaRegistration 
{
    public override string AreaName 
    {
        get 
        {
            return "Blog";
        }
    }

    public override void RegisterArea(AreaRegistrationContext context) 
    {
        context.MapRoute(
            "Blog_default",
            "Blog/{action}/{id}",
            new { controller ="blog", action = "Index", id = UrlParameter.Optional }
        );
    }
}

修改路由来源

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

修改

在发言/ u / Eric Philips之后,我想出了一个解决方案。解决方案是重写我的文件结构到MVC兼容。设置控制器并将index.cshtml页面移动到有效位置后,/ Blog /工作正常。

  public class BlogController : Controller
{
    // GET: Blog/Blog
    public ActionResult Index()
    {
        return View();
    }
}

0 个答案:

没有答案
相关问题