如何在MVC 5中用数据库中的字符串重写URL? [英] How to rewrite URL by a string from database in MVC 5?
问题描述
我在数据库中有一个表,其中一些列是: [id] , [title] , [data]
我创建了一个允许用户发布新帖子的页面。示例:如果我发布标题为的新主题为:这是我的标题。提交后,标题将转换为链接
< a href = / NewThread / Here-is-my-title > 这是我的标题 < / a >
我已经完成了这个控制。
现在,当我点击链接时,它将从列 [title] 加载数据在数据库中并将其放在URL中。像这样:
http://www.example.com/NewThread/Here-is-my-title
你能告诉我怎么样吗?要做到这一点?
我正在使用MVC 5.如果我将标题转换为标签,它将需要一个控制器和一个动作来执行。如上面的链接,控制器名称是NewThread,操作名称是Here-is-my-title。
但是我的问题是:这里是我的标题不是动作,它是从列 [标题]中加载的数据在数据库中。
请帮帮我。谢谢。
在.Layout.cshtml中定义函数
< script>
( document )。ready( function (){
(' [data-action = navigate]')。on( ' 点击',功能(e) {
e.preventDefault();
var url =
I have a table in database with some columns are: [id], [title], [data]
I have created a page to allow user posting new thread. Example: If I post a new thread with the title is: "Here is my title". After submitting, the title will be converted to a link
<a href="/NewThread/Here-is-my-title">Here is my title</a>
I have done that control.
Now, when I click on the link, it will load data from column [title] in database and put it in URL. Like this:
http://www.example.com/NewThread/Here-is-my-title
Can you tell me how to do that?
I'm using MVC 5. If I convert the title to an a tag, it will need a controller and an action to execute. As the link above, the controller name is "NewThread", and the action name is "Here-is-my-title".
But my problem is: "Here-is-my-title" is not an action, it's a data which was loaded from column [title] in database.
Please help me. Thank you.
Define the function in .Layout.cshtml
<script>
(document).ready(function () {
('[data-action=navigate]').on('click', function (e) { e.preventDefault(); var url =
这篇关于如何在MVC 5中用数据库中的字符串重写URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!