相关疑难解决方法(0)

如何在ASP.NET MVC中模拟Server.Transfer?

在ASP.NET MVC中,您可以非常轻松地返回重定向ActionResult:

 return RedirectToAction("Index");

 or

 return RedirectToRoute(new { controller = "home", version = Math.Random() * 10 });
Run Code Online (Sandbox Code Playgroud)

这实际上会提供HTTP重定向,这通常很好.但是,当使用谷歌分析时,这会导致很大的问题,因为最初的引用会丢失,所以谷歌不知道你来自哪里.这会丢失有用的信息,例如任何搜索引擎术语.

作为旁注,此方法的优点是可以删除任何可能来自广告系列但仍允许我捕获服务器端的参数.将它们留在查询字符串中会导致人们加入书签或推特或博客链接,而这些链接不应该存在.我已经多次看到这样的情况,人们在我们的网站上链接了包含广告系列ID的链接.

无论如何,我正在为网站的所有传入访问写一个"网关"控制器,我可能会重定向到不同的地方或替代版本.

现在我更关心谷歌(比意外的书签),我希望能够发送访问/该页面的人,如果他们去了/home/7,这是主页的第7版.

就像我之前说的如果我这样做,我失去了谷歌分析引用者的能力:

 return RedirectToAction(new { controller = "home", version = 7 });
Run Code Online (Sandbox Code Playgroud)

我真正想要的是一个

 return ServerTransferAction(new { controller = "home", version = 7 });
Run Code Online (Sandbox Code Playgroud)

这将使我看到没有客户端重定向的视图.我不认为这样的事情存在.

目前我能想到的最好的事情是HomeController.Index(..)在我的GatewayController.IndexAction中复制整个控制器逻辑.这意味着我必须'Views/Home'进入,'Shared'因此它是可访问的.肯定有更好的办法??..

asp.net-mvc server.transfer

123
推荐指数
8
解决办法
7万
查看次数

ASP.NET MVC CMS数据库的动态路由

基本上我有一个使用ASP.NET MVC构建的CMS后端,现在我正在进入前端站点,需要能够根据输入的路由从我的cms数据库加载页面.

因此,如果用户输入domain.com/students/information,MVC将查看页面表以查看是否存在具有与学生/信息匹配的永久链接的页面,如果是,则将重定向到页面控制器然后加载页面来自数据库的数据并将其返回到视图以供显示.

到目前为止,我已尝试捕获所有路径,但它仅适用于两个URL段,因此/学生/信息,但不是/ students/information/fall.我在网上找不到任何关于如何实现这一点的内容,所以我会在这里问一下,在我找到并开源ASP.NET MVC cms并剖析代码之前.

这是我到目前为止的路由配置,但我觉得有更好的方法来做到这一点.

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

        // Default route to handle core pages
        routes.MapRoute(null,"{controller}/{action}/{id}",
                        new { action = "Index", id = UrlParameter.Optional },                  
                        new { controller = "Index" }
        );

        // CMS route to handle routing to the PageController to check the database for the route.


        var db = new MvcCMS.Models.MvcCMSContext();
        //var page = db.CMSPages.Where(p => p.Permalink == )
        routes.MapRoute(
            null,
            "{*.}",
            new { controller = "Page", action = "Index" } …
Run Code Online (Sandbox Code Playgroud)

c# routes asp.net-mvc-4

67
推荐指数
1
解决办法
3万
查看次数

启动后更改MVC6的路由集合

在MVC-5中,我可以routetable通过访问来编辑初始启动之后RouteTable.Routes.我希望在MVC-6中也这样做,这样我就可以在运行时添加/删除路由(对CMS很有用).

在MVC-5中执行此操作的代码是:

using (RouteTable.Routes.GetWriteLock())
{
    RouteTable.Routes.Clear();

    RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    RouteTable.Routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}
Run Code Online (Sandbox Code Playgroud)

但我RouteTable.Routes在MVC-6中找不到或类似的东西.知道如何在运行时更改路径集合吗?


我想在CMS中创建页面时使用此原则添加额外的URL.

如果你有一个类:

public class Page
{
    public int Id { get; set; }
    public string Url { get; set; }
    public string Html { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

和控制器像:

public class CmsController : Controller
{
    public ActionResult Index(int id)
    {
        var page = DbContext.Pages.Single(p => p.Id …
Run Code Online (Sandbox Code Playgroud)

c# routes asp.net-mvc-routing asp.net-core-mvc asp.net-core

22
推荐指数
1
解决办法
6851
查看次数

在ASP.NET 5(vNext)MVC 6中添加自定义IRouter

我正在尝试将此示例RouteBase实现转换为与MVC 6一起使用.我已经通过遵循路由项目中的示例来解决大部分问题,但是我正在了解如何Task从方法返回异步.我真的不在乎它是否实际上是异步的(为任何可以提供答案的人欢呼),现在我只想让它运转起来.

我有传出的路线功能(ActionLink当我输入路线值时,意味着工作正常).问题在于RouteAsync方法.

public Task RouteAsync(RouteContext context)
{
    var requestPath = context.HttpContext.Request.Path.Value;

    if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
    {
        // Trim the leading slash
        requestPath = requestPath.Substring(1);
    }

    // Get the page that matches.
    var page = GetPageList()
        .Where(x => x.VirtualPath.Equals(requestPath))
        .FirstOrDefault();

    // If we got back a null value set, that means the URI did not match
    if (page != null)
    {
        var routeData = new RouteData();

        // This …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc asp.net-mvc-routing asp.net-core-mvc asp.net-core

20
推荐指数
1
解决办法
6087
查看次数

为什么在asp.net mvc中的常用路由之前首先映射特殊路由?

来自www:

...路由引擎将采用与提供的URL匹配的第一个路由,并尝试使用该路由中的路由值.因此,应首先在表中添加不太常见或更专业的路由,而稍后应添加更一般的路由......

我为什么要先绘制专业路线?有人可以举个例子,我可以看到"地图常见路线优先"的失败吗?

asp.net-mvc url-routing asp.net-mvc-routing asp.net-mvc-5 asp.net-mvc-5.2

5
推荐指数
1
解决办法
3527
查看次数

asp net MVC 5 SiteMap - 将面包屑构建到另一个控制器

我为我的 asp net mvc 电子商务开发面包屑。我有一个用于我的类别的控制器。它看起来像这样:

 public class CategoryController : AppController
    {

    public ActionResult Index(string cat1, string cat2, string cat3, int? page)
        {

... some code

       // build breadcrumbs from parent cats
        int indexer = 0;
        foreach(var item in parCategories) //parCategories - list of parent categories
        {
            string currCatIndex = new StringBuilder().AppendFormat("category{0}", indexer + 1).ToString(); //+2 cause arr index begins from 0
            var currNode = SiteMaps.Current.FindSiteMapNodeFromKey(currCatIndex);           
            currNode.Title= parCategories.ElementAt(indexer).Name;
            indexer++;
        }

        string finalCatIndex = new StringBuilder().AppendFormat("category{0}", CategoryDepth + 1).ToString();
        var node = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net asp.net-mvc asp.net-mvc-routing asp.net-mvc-5

1
推荐指数
1
解决办法
3613
查看次数