【问题标题】:How to use a controller for generate URLs SEO all links如何使用控制器生成 URL SEO 所有链接
【发布时间】:2018-07-25 16:57:16
【问题描述】:

我有一张桌子,我想用它来制作目录。

我的桌子是这样的:

Id Product Description Price

我将创建一个索引,在其中列出所有产品及其名称,并通过链接链接到我有其描述的视图,我知道该怎么做,但我不知道怎么做使索引的每个链接自动或通用接收驱动程序例如:

<a href="https://example.tld/product/id/name-of-product">name of product</a>

我认为我不必创建控制器产品名称和另一个产品名称我想我发送 控制器 产品的地方有一些通用的东西 acion 索引

 示例:ProductController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MySite.Controllers
    {
        public class ProductyController: Controller
        {
            // GET: Product
            public ActionResult Index (int id, string productstring)
            {
                return Content (productstring + "example fooo bar ..");
                // or
                return View ("my_generic_view_styled_css.cshtml");
            }
        }
    }

这样当你输入https://example.tld/product/id/foo-bar-any

我用我的信息返回了我的 index.cshtml,我知道如何填写它,但我不知道如何处理 SEO 的网址

我的问题是没有用链接创建索引,问题是直接点击或转到url没有错误404

【问题讨论】:

  • 那么为什么不在 Index 函数中创建一个包含所有链接和描述的视图模型,以及一个创建 html 的视图呢?你知道,就像任何其他普通视图/视图模型一样。
  • 我的问题不是用链接创建索引,问题是直接点击或者去url没有错误404

标签: c# model-view-controller clean-urls


【解决方案1】:

您似乎遇到了路由问题。

这看起来如何:

[HttpGet("/link/{product}/{id}/{something}")]
public void Link(string product, string id, string something)
{

}

这会将 url 的各个部分解码为您需要的单独部分。

【讨论】:

    【解决方案2】:

    如果有人为您服务,最终的解决方案如下

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

    另一个技巧是调用

        routes.MapMvcAttributeRoutes(); //super important for solution
    

    紧接着

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    

    RouteConfig.cs

    【讨论】:

      猜你喜欢
      • 2012-10-02
      • 2019-07-11
      • 1970-01-01
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多