ASP.NET MVC Razor,使用url/profile/{username}生成配置文件 [英] ASP.NET MVC Razor, generate profile using url /profile/{username}

查看:49
本文介绍了ASP.NET MVC Razor,使用url/profile/{username}生成配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试基于url创建用户个人资料.我知道如何从网址中获取参数,例如www.domain.com/profile?id=username但是我想保持它对SEO的友好,而是从url格式中获取价值:www.domain.com/profile/用户名

I am trying to create user profiles based on url. I know how to get paramaters from url like www.domain.com/profile?id=username But I would like to keep it SEO friendly and instead get value from url format: www.domain.com/profile/username

我正在使用Visual Studio 2017中的MVC Razor和不适当的MSSQL数据库,而是使用用户表实现MySQL.

I am using MVC Razor from Visual Studio 2017 and disgarded MSSQL database, instead implemented MySQL with a user table.

我认为它应该如何工作.用户访问网页www.domain.com/profile/user235

My thoughts how it should work. User visits web page www.domain.com/profile/user235

服务器加载控制器并获取输入"user235"

the server loads up the controller and gets the input "user235"

控制器将输入发送到.cshtml页,.cshtml页执行查询到MySQL数据库以获取用户数据数组并用数据填充页面.

the controller sends the input to the .cshtml page and the .cshtml page performs query to the MySQL database getting the user data array and filling the page with the data.

我的想法正确吗?有什么建议或建议,我应该如何处理?

Is my thoughts correct? Any advice or suggestions how I should handle this?

在以下位置找到了解决方案:

Found the solution at: https://docs.microsoft.com/en-us/aspnet/web-pages/overview/routing/creating-readable-urls-in-aspnet-web-pages-sites

@UrlData [0] .ToString()

@UrlData[0].ToString()

这将在/剃须刀页面中获得任何值例子:profile.cshtml具有字符串用户名= @UrlData [0] .ToString()

This will get any value after / in razor pages example: profile.cshtml has string username = @UrlData[0].ToString()

www.domain.com/profile/约翰

www.domain.com/profile/John

@UrlData [0] .ToString()=约翰"

@UrlData[0].ToString() = "John"

如果您想要类似www.domain.com/profile/John/blog43

If you want something like www.domain.com/profile/John/blog43

字符串用户名= @UrlData [0] .ToString()字符串blogid = @UrlData [1] .ToString()

string username = @UrlData[0].ToString() string blogid = @UrlData[1].ToString()

使用此信息,您可以查询数据库以获取信息...确保对任何类型的sql注入都验证用户名和博客输入.

And with this information you can query the database to get information... Make sure to validate the username and blog input for any kind of sql injections.

推荐答案

您可以定义路由定义以获取类似seo的网址

You can define a route definition to get seo friendly urls like that

这是使用属性路由的方法

Here is how you do it with attribute routing

public class ProfileController : Controller
{
    [Route("Profile/{userId}")]
    public ActionResult Index(string userId)
    {
        return Content("Proile for " + userId);
    }
}

因此,当用户访问/yourSite/profile/scott 时,将执行Profile控制器的Index操作,并且该操作的userId参数中将提供来自url(scott)的用户名值方法.在这里,我只是返回一个字符串"scott的配置文件".但是您可以对其进行查询以查询数据库以获得相应的记录,然后使用视图模型将该信息传递给视图.

So when you user access /yourSite/profile/scott the Index action of Profile controller will be executed and the username value from the url (scott) will be available in the userId parameter of the action method. Here i am simply returning a string "Profile for scott". But you can udpate it to query your database to get the corresponding record and pass that information to a view using a view model.

如果使用的是Razor页面,则将创建一个Profile页面及其模型并指定路线

If you are using Razor pages, you will create a Profile page and it's model and specify the route

@page "{id:int}"
@model ProfileModel
@{
    ViewData["Title"] = "Profile";
}    
<h3>@Model.Message</h3>

并在您的 Profile.cshtml.cs

public class ProfileModel : PageModel
{
    public string Message { get; set; }

    public void OnGet(string id)
    {
        Message = "Profile of "+ id;
    }
}

就像我上面提到的,我只是打印一个带有URL中传递的用户名的字符串.但是您可以更改它,以从db表中获取数据.

Like i mentioned above, i am just printing a string with the passed username from the url. But you can change it to get your data from your db table.

这篇关于ASP.NET MVC Razor,使用url/profile/{username}生成配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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