MVC动态视图从以下网址弹头 [英] MVC Dynamic Views From Url Slug

查看:80
本文介绍了MVC动态视图从以下网址弹头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我建设我的第一个.NET MVC的Web应用程序和热爱简单性和灵活性。不过,我是来我的第一个拦路虎。

I am building my first .NET MVC web application and loving the simplicity and flexibility. I have however come to my first stumbling block.

我有一个项目列表,如果您没有登录,你会看到一个preVIEW链接,我想的链接直接到类似如下:

I have a list of items and if you are not logged in you will see a preview link I would like the link to direct to something like below:

/preview/unique-slug

该视图应该然后让我从数据库(本质上是一个详细信息页)

The view should then allow me to display the contents from the database (essentially a details page)

我不知道如何处理这也不是我应该Google'ing因为我得到的结果是穷人。

I am not sure how to approach this nor what I should be Google'ing as the results I got were poor.

任何指针吗?

推荐答案

这是我在过去做到了这一点的方式:

This is the way I've done this in the past:

您需要在数据库中存储与所述塞的柱,每个项被称为通过。然后,创建类似下面的路线:

You need to store in the database a column with the slug that each item is referred to by. Then, create a route like the following:

routes.MapRoute("PreviewLink",
    "/preview/{slug}",
    new { controller = "Preview", action = "Details" }
);

因此​​,这将呼吁详细信息方法 previewController 像这样:

class PreviewController : Controller
{
    public ActionResult Details(string slug)
    {
        var model = db.GetItemBySlug(slug);
        return View("Details", model);
    }
}

基本上,你会得到蛞蝓作为一个字符串在你的操作方法,在数据库中查询与相应的塞的项目,并显示它。从本质上讲,我们只需更换一个整数 ID 用字符串蛞蝓

希望这将清除的东西了你!

Hopefully that clears things up for you!

这篇关于MVC动态视图从以下网址弹头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆