不同领域和 ASP.NET MVC 应用程序的 URL 缩短器
创始人
2025-01-09 10:00:16
0

以下是一个使用ASP.NET MVC框架创建URL缩短器的示例解决方案:

  1. 创建一个新的ASP.NET MVC应用程序项目。
  2. 在Models文件夹中创建一个名为UrlShortener.cs的模型类,如下所示:
using System.ComponentModel.DataAnnotations;

public class UrlShortener
{
    [Key]
    public int Id { get; set; }

    [Required]
    public string OriginalUrl { get; set; }

    [Required]
    public string ShortenedUrl { get; set; }
}
  1. 在Controllers文件夹中创建一个名为UrlShortenerController.cs的控制器类,包含处理URL缩短的动作方法,如下所示:
using System;
using System.Linq;
using System.Web.Mvc;

public class UrlShortenerController : Controller
{
    private ApplicationDbContext db = new ApplicationDbContext();

    public ActionResult Index()
    {
        var urls = db.UrlShorteners.ToList();
        return View(urls);
    }

    public ActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Create(UrlShortener urlShortener)
    {
        if (ModelState.IsValid)
        {
            urlShortener.ShortenedUrl = GenerateShortUrl();
            db.UrlShorteners.Add(urlShortener);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(urlShortener);
    }

    public ActionResult Redirect(string shortenedUrl)
    {
        var url = db.UrlShorteners.FirstOrDefault(u => u.ShortenedUrl == shortenedUrl);
        if (url != null)
        {
            return Redirect(url.OriginalUrl);
        }

        return RedirectToAction("Index");
    }

    private string GenerateShortUrl()
    {
        // 实现自定义的URL缩短算法
        // 返回一个唯一的短URL
    }
}
  1. 在Views文件夹中创建一个名为UrlShortener文件夹,在该文件夹中创建以下视图:
  • Index.cshtml:显示所有URL缩短器的列表。
@model List

@foreach (var url in Model)
{
    

Original URL: @url.OriginalUrl Shortened URL: @Html.ActionLink(url.ShortenedUrl, "Redirect", new { shortenedUrl = url.ShortenedUrl })

}
  • Create.cshtml:用于创建新的URL缩短器。
@model UrlShortener

@using (Html.BeginForm())
{
    @Html.LabelFor(model => model.OriginalUrl)
    @Html.TextBoxFor(model => model.OriginalUrl)
    
    
}
  1. 在Global.asax.cs文件中添加以下路由配置:
using System.Web.Mvc;
using System.Web.Routing;

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        // 其他配置代码
        
        // 添加URL缩短器路由
        RouteTable.Routes.MapRoute(
            name: "Redirect",
            url: "{shortenedUrl}",
            defaults: new { controller = "UrlShortener", action = "Redirect" }
        );
    }
}

以上代码是一个基本的URL缩短器示例,可以根据实际需求进行修改和扩展。

相关内容

热门资讯

透视苹果版"werp... 透视苹果版"werplan怎么作弊"详细辅助高科技教程(从前有挂);《WPK辅助透视》‌:支持手机实...
透视玄学"hhpok... 透视玄学"hhpoker德州机器人"详细辅助详细教程(往昔有挂);建议优先通过hhpoker德州机器...
透视透视"hhpok... 透视透视"hhpoker软件安装包"详细辅助新2025教程(原生是真的有挂);hhpoker软件安装...
透视智能ai"hhp... 1、透视智能ai"hhpoker有后台操控吗"详细辅助攻略方法(好像是真的有挂)。2、hhpoker...
透视肯定"we po... 1、透视肯定"we poker免费辅助器"详细辅助解密教程(最初是有挂)。2、we poker免费辅...
透视有挂"epoke... 1、透视有挂"epoker有透视吗"详细辅助科技教程(原先是真的有挂)。2、epoker有透视吗透视...
透视最新"智星德州插... 透视最新"智星德州插件"详细辅助实用技巧(切实是有挂);支持多人共享记分板与复盘,通过邀请好友或转让...
透视攻略"wepok... 透视攻略"wepoker辅助器安装包"详细辅助透明挂教程(原先真的有挂);大神普及一款德州ai内幕,...
透视有挂"wpk私人... 透视有挂"wpk私人辅助"详细辅助普及教程(原本是真的有挂);wpk私人辅助软件透明挂作为一款wpk...
透视教学"wepok... 透视教学"wepoker辅助是真的吗"详细辅助必赢方法(真是是真的有挂);支持2-10人实时对战,虚...