【问题标题】:Redirecting responsibility from short to full URL in another controller在另一个控制器中将责任从短 URL 重定向到完整 URL
【发布时间】:2018-08-29 17:26:33
【问题描述】:

作为作业,我必须做一个简单的 URL 缩短器,我可以在其中添加完整链接到列表,由 Hashids.net library 处理,我得到一个 URL 的简短版本。

我现在有类似的东西,但我一直坚持将其重定向回完整链接。

我想添加一个新的控制器,它负责将短 URL 重定向到完整 URL。单击短 URL 后,它应该转到 localhost:xxxx/ShortenedUrl,然后重定向到完整链接。任何提示如何创建这个?

我试图通过重定向控制器中的@Html.ActionLink(@item.ShortenedLink, "Index", "Redirect") 和return Redirect(fullLink) 来执行此操作,但它没有按我预期的那样工作。

还有一个关于路由的问题,点击短 URL 后如何实现它会给我localhost:XXXX/ShortenedURL(即localhost:XXXX/FSIAOFJO2@)。现在我有

<a href="@item.ShortenedLink">@Html.DisplayFor(model => item.ShortenedLink)</a> 

和

app.UseMvc(routes =>
{ 
    routes.MapRoute("default", "{controller=Link}/{action=Index}");
});

但它给了我localhost:XXXX/Link/ShortenedURL,所以我想在 URL 中省略这个链接。

查看(带有短网址部分):

 <td>@Html.ActionLink(item.ShortenedLink,"GoToFull","Redirect", new { target = "_blank" }))</td>

链接控制器:

public class LinkController : Controller
{
    private ILinksRepository _repository;

    public LinkController(ILinksRepository linksRepository)
    {
        _repository = linksRepository;
    }

    [HttpGet]
    public IActionResult Index()
    {
        var links = _repository.GetLinks();
        return View(links);
    }

    [HttpPost]
    public IActionResult Create(Link link)
    {
        _repository.AddLink(link);
        return Redirect("Index");
    }

    [HttpGet]
    public IActionResult Delete(Link link)
    {
        _repository.DeleteLink(link);
        return Redirect("Index");
    }
}

我正在尝试做的重定向控制器:

private ILinksRepository _repository;

public RedirectController(ILinksRepository linksRepository)
{
    _repository = linksRepository;
}

public IActionResult GoToFull()
{
    var links = _repository.GetLinks();
    return Redirect(links[0].FullLink);
}

有没有更好的方法来访问重定向控制器中的链接列表?

【问题讨论】:

  • 它是 Asp .Net Core

标签: c# asp.net-core asp.net-core-mvc


【解决方案1】:

这是我的建议,通过 AJAX 触发链接,这是工作示例:

这是通过模型绑定的HTML元素:

@Html.ActionLink(Model.ShortenedLink, "", "", null, 
new { onclick = "fncTrigger('" + "http://www.google.com" + "');" })

这是 javascript ajax 代码:

function fncTrigger(id) {

            $.ajax({
                url: '@Url.Action("TestDirect", "Home")',
                type: "GET",
                data: { id: id },
                success: function (e) {
                },
                error: function (err) {
                    alert(err);
                },
            });
    }

然后在你的控制器上接收ajax点击:

 public ActionResult TestDirect(string id)
    {
        return JavaScript("window.location = '" + id + "'");
    }

基本上我在这里所做的是,在我单击链接后,它将调用 TestDirect 操作,然后将其重定向到使用传递的 url 参数。您可以在此操作中进行转换。

【讨论】:

    【解决方案2】:

    要创建动态数据驱动的 URL,您需要创建一个自定义 IRouter。以下是它的实现方法:

    CachedRoute&lt;TPrimaryKey&gt;

    这是一个可重用的通用类,它将一组动态提供的 URL 映射到单个操作方法。您可以注入 ICachedRouteDataProvider&lt;TPrimaryKey&gt; 来提供数据(主键映射的 URL)。

    缓存数据以防止多个同时请求使数据库过载(路由在每个请求上运行)。默认缓存时间为 15 分钟,但您可以根据需要进行调整。

    如果您希望它“立即”采取行动,您可以构建一个更高级的缓存,该缓存在成功更新其中一条记录的数据库后立即更新。也就是说,相同的操作方法会同时更新数据库和缓存。

    using Microsoft.AspNetCore.Routing;
    using Microsoft.Extensions.Caching.Memory;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    public class CachedRoute<TPrimaryKey> : IRouter
    {
        private readonly string _controller;
        private readonly string _action;
        private readonly ICachedRouteDataProvider<TPrimaryKey> _dataProvider;
        private readonly IMemoryCache _cache;
        private readonly IRouter _target;
        private readonly string _cacheKey;
        private object _lock = new object();
    
        public CachedRoute(
            string controller,
            string action,
            ICachedRouteDataProvider<TPrimaryKey> dataProvider,
            IMemoryCache cache,
            IRouter target)
        {
            if (string.IsNullOrWhiteSpace(controller))
                throw new ArgumentNullException("controller");
            if (string.IsNullOrWhiteSpace(action))
                throw new ArgumentNullException("action");
            if (dataProvider == null)
                throw new ArgumentNullException("dataProvider");
            if (cache == null)
                throw new ArgumentNullException("cache");
            if (target == null)
                throw new ArgumentNullException("target");
    
            _controller = controller;
            _action = action;
            _dataProvider = dataProvider;
            _cache = cache;
            _target = target;
    
            // Set Defaults
            CacheTimeoutInSeconds = 900;
            _cacheKey = "__" + this.GetType().Name + "_GetPageList_" + _controller + "_" + _action;
        }
    
        public int CacheTimeoutInSeconds { get; set; }
    
        public async Task RouteAsync(RouteContext context)
        {
            var requestPath = context.HttpContext.Request.Path.Value;
    
            if (!string.IsNullOrEmpty(requestPath) && requestPath[0] == '/')
            {
                // Trim the leading slash
                requestPath = requestPath.Substring(1);
            }
    
            // Get the page id that matches.
            TPrimaryKey id;
    
            //If this returns false, that means the URI did not match
            if (!GetPageList().TryGetValue(requestPath, out id))
            {
                return;
            }
    
            //Invoke MVC controller/action
            var routeData = context.RouteData;
    
            // TODO: You might want to use the page object (from the database) to
            // get both the controller and action, and possibly even an area.
            // Alternatively, you could create a route for each table and hard-code
            // this information.
            routeData.Values["controller"] = _controller;
            routeData.Values["action"] = _action;
    
            // This will be the primary key of the database row.
            // It might be an integer or a GUID.
            routeData.Values["id"] = id;
    
            await _target.RouteAsync(context);
        }
    
        public VirtualPathData GetVirtualPath(VirtualPathContext context)
        {
            VirtualPathData result = null;
            string virtualPath;
    
            if (TryFindMatch(GetPageList(), context.Values, out virtualPath))
            {
                result = new VirtualPathData(this, virtualPath);
            }
    
            return result;
        }
    
        private bool TryFindMatch(IDictionary<string, TPrimaryKey> pages, IDictionary<string, object> values, out string virtualPath)
        {
            virtualPath = string.Empty;
            TPrimaryKey id;
            object idObj;
            object controller;
            object action;
    
            if (!values.TryGetValue("id", out idObj))
            {
                return false;
            }
    
            id = SafeConvert<TPrimaryKey>(idObj);
            values.TryGetValue("controller", out controller);
            values.TryGetValue("action", out action);
    
            // The logic here should be the inverse of the logic in 
            // RouteAsync(). So, we match the same controller, action, and id.
            // If we had additional route values there, we would take them all 
            // into consideration during this step.
            if (action.Equals(_action) && controller.Equals(_controller))
            {
                // The 'OrDefault' case returns the default value of the type you're 
                // iterating over. For value types, it will be a new instance of that type. 
                // Since KeyValuePair<TKey, TValue> is a value type (i.e. a struct), 
                // the 'OrDefault' case will not result in a null-reference exception. 
                // Since TKey here is string, the .Key of that new instance will be null.
                virtualPath = pages.FirstOrDefault(x => x.Value.Equals(id)).Key;
                if (!string.IsNullOrEmpty(virtualPath))
                {
                    return true;
                }
            }
            return false;
        }
    
        private IDictionary<string, TPrimaryKey> GetPageList()
        {
            IDictionary<string, TPrimaryKey> pages;
    
            if (!_cache.TryGetValue(_cacheKey, out pages))
            {
                // Only allow one thread to poplate the data
                lock (_lock)
                {
                    if (!_cache.TryGetValue(_cacheKey, out pages))
                    {
                        pages = _dataProvider.GetPageToIdMap();
    
                        _cache.Set(_cacheKey, pages,
                            new MemoryCacheEntryOptions()
                            {
                                Priority = CacheItemPriority.NeverRemove,
                                AbsoluteExpirationRelativeToNow = TimeSpan.FromSeconds(this.CacheTimeoutInSeconds)
                            });
                    }
                }
            }
    
            return pages;
        }
    
        private static T SafeConvert<T>(object obj)
        {
            if (typeof(T).Equals(typeof(Guid)))
            {
                if (obj.GetType() == typeof(string))
                {
                    return (T)(object)new Guid(obj.ToString());
                }
                return (T)(object)Guid.Empty;
            }
            return (T)Convert.ChangeType(obj, typeof(T));
        }
    }
    

    LinkCachedRouteDataProvider

    这里我们有一个简单的服务,它从数据库中检索数据并将其加载到字典中。最复杂的部分是为了在服务中使用DbContext 而需要设置的范围。

    public interface ICachedRouteDataProvider<TPrimaryKey>
    {
        IDictionary<string, TPrimaryKey> GetPageToIdMap();
    }
    
    public class LinkCachedRouteDataProvider : ICachedRouteDataProvider<int>
    {
        private readonly IServiceProvider serviceProvider;
    
        public LinkCachedRouteDataProvider(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider
                ?? throw new ArgumentNullException(nameof(serviceProvider));
        }
    
        public IDictionary<string, int> GetPageToIdMap()
        {
            using (var scope = serviceProvider.CreateScope())
            {
                var dbContext = scope.ServiceProvider.GetService<ApplicationDbContext>();
    
                return (from link in dbContext.Links
                        select new KeyValuePair<string, int>(
                            link.ShortenedLink.Trim('/'),
                            link.Id)
                        ).ToDictionary(pair => pair.Key, pair => pair.Value);
            }
        }
    }
    

    RedirectController

    我们的重定向控制器接受主键作为 id 参数,然后查找数据库记录以获取要重定向到的 URL。

    public class RedirectController
    {
        private readonly ApplicationDbContext dbContext;
    
        public RedirectController(ApplicationDbContext dbContext)
        {
            this.dbContext = dbContext
                ?? throw new ArgumentNullException(nameof(dbContext));
        }
    
        public IActionResult GoToFull(int id)
        {
            var link = dbContext.Links.FirstOrDefault(x => x.Id == id);
            return new RedirectResult(link.FullLink);
        }
    }
    

    在生产场景中,您可能希望将其设为永久重定向return new RedirectResult(link.FullLink, true),但这些会被浏览器自动缓存,这使得测试变得困难。

    Startup.cs

    我们在 DI 容器中设置 DbContext、内存缓存和 LinkCachedRouteDataProvider 以供以后使用。

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
        services.AddMvc();
    
        services.AddMemoryCache();
        services.AddSingleton<LinkCachedRouteDataProvider>();
    }
    

    然后我们使用CachedRoute&lt;TPrimaryKey&gt; 设置路由,提供所有依赖项。

    app.UseMvc(routes =>
    {
        routes.Routes.Add(new CachedRoute<int>(
            controller: "Redirect",
            action: "GoToFull",
            dataProvider: app.ApplicationServices.GetService<LinkCachedRouteDataProvider>(),
            cache: app.ApplicationServices.GetService<IMemoryCache>(),
            target: routes.DefaultHandler)
            // Set to 60 seconds of caching to make DB updates refresh quicker
            { CacheTimeoutInSeconds = 60 });
    
        routes.MapRoute(
            name: "default",
            template: "{controller=Home}/{action=Index}/{id?}");
    });
    

    要在用户界面上构建这些短 URL,您可以像使用任何其他路由一样使用标签助手(或 HTML 助手):

    <a asp-area="" asp-controller="Redirect" asp-action="GoToFull" asp-route-id="1">
        @Url.Action("GoToFull", "Redirect", new { id = 1 })
    </a>
    

    生成如下:

    <a href="/M81J1w0A">/M81J1w0A</a>
    

    您当然可以使用模型在生成时将id 参数传递到您的视图中。

    <a asp-area="" asp-controller="Redirect" asp-action="GoToFull" asp-route-id="@Model.Id">
        @Url.Action("GoToFull", "Redirect", new { id = Model.Id })
    </a>
    

    我做了一个Demo on GitHub。如果您在浏览器中输入短 URL,它们将被重定向到长 URL。

    • M81J1w0A -> https://maps.google.com/
    • r33NW8K -> https://stackoverflow.com/

    我没有创建任何视图来更新数据库中的 URL,但这种类型的东西在几个教程中都有介绍,例如 Get started with ASP.NET Core MVC and Entity Framework Core using Visual Studio,而且看起来你在这部分没有问题.

    参考资料:

    【讨论】:

      猜你喜欢
      • 2022-11-16
      • 1970-01-01
      • 2012-05-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-10
      • 1970-01-01
      • 2013-05-28
      相关资源
      最近更新 更多