首页 > 解决方案 > 如何使用控制器生成 URL SEO 所有链接

问题描述

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

我的桌子是这样的:

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

标签: c#model-view-controllerclean-urls

解决方案


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

    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 中


推荐阅读