不同领域和 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缩短器示例,可以根据实际需求进行修改和扩展。

相关内容

热门资讯

透视游戏!智星菠萝辅助怎么买,... 【福星临门,好运相随】;透视游戏!智星菠萝辅助怎么买,hh poker透视器下载,技巧教程(都是是真...
透视真的!wepoker透视底... 透视真的!wepoker透视底牌脚本,红龙poker透视,技巧知识分享(有挂总结)1、下载好wepo...
透视教程!wpk透视脚本链接,... WePoke高级策略深度解析‌;透视教程!wpk透视脚本链接,wepoker有没有插件,高科技教程(...
透视了解!wpk有脚本吗,wp... 1、透视了解!wpk有脚本吗,wpk透视工作室,科普攻略(有挂介绍)。2、wpk透视工作室透视辅助简...
透视脚本!wpk透视辅助方法,... 相信很多朋友都在电脑上玩过wpk透视辅助方法吧,但是很多朋友都在抱怨用电脑玩起来不方便。为此小编给大...
透视透视!智星德州菠萝透视插件... 透视透视!智星德州菠萝透视插件工具,德普之星辅助功能如何设置,查到实测辅助(有挂规律)1、每个玩家都...
透视美元局!hhpoker万能... 1、透视美元局!hhpoker万能辅助器,wepoker私人局辅助挂,详细教程(原本真的是有挂)2、...
透视辅助!hhpoker免费透... 一、hhpoker免费透视脚本简介了解软件请加微:136704302hhpoker免费透视脚本是一款...
透视黑科技!aapoker辅助... WePoker透视辅助工具核心要点解析‌,透视黑科技!aapoker辅助器怎么用,hhpoker是真...
透视ai代打!aapoker辅... 透视ai代打!aapoker辅助器是真的吗,wepoker脚本下载,安装程序教程(有挂方法)1、实时...