c# - 如何使用控制器生成 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
解决方案
如果有人为您服务,最终的解决方案如下
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 中
推荐阅读
- python - 将分隔项目的python列表转换为熊猫数据框
- celery - 在 Celery 任务中,数据库调度程序是否足够,或者我是否还需要指定 run_every 属性?
- typescript - TypeScript 没有看到在变量中执行了 undefined\null 值检查
- godot - 从 2D 编辑器添加不同的布局选项
- html - 如何在网页上显示搜索结果片段
- splunk - 来自通用转发器的 Splunk 熟格式
- rust - 使用 actix-web 2.0 提供静态文件
- java - 为什么使用多线程时静态代码中的 Class.forName() 会终止进程?
- flutter - 如何使用“flutter_open_whatsapp”包发送图像?
- python - 创建一个在每个块上循环的网格系统?