如何使用控制器生成 URL SEO 所有链接

How to use a controller for generate URLs SEO all links

我有一个table,我想用它来制作目录。

我的table是这样的:

Id Product Description Price

我要创建一个索引,在其中列出所有产品及其名称,它们 link 我用 link 到一个视图,我有它们的描述,我知道如何做所有这些,但我不知道如何使索引的每个 link 自动或一般接收驱动程序,例如:

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

我不认为我必须创建一个控制器产品名称和另一个产品名称我想我发送 控制器[=36= 的地方有一些通用的东西] product accion index

示例: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 我知道如何填写它,但我不知道如何处理 urls搜索引擎优化

我的问题不是用link创建索引,问题是点击或直接进入url没有错误404

您的路由似乎有问题。

这看起来如何:

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

}

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

万一有人为你服务,最后的解决方案如下

    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