ASP.NET MVC Routing Constructs Links Based on the Current URL

I have urls that look like this

~\articles\energy\topweek

~\articles\metals\latestpopular

where the second line of url is a category and the third is a filter

so the route is as follows

    routes.MapRoute("ArticleFilter",
     "articles/{category}/{filter}",
  new { controller="Article", action="Filter" })

This is pretty easy and everything works fine.

So let's say if I look at the articles {category} \ default view.

How to create links to display the current category using filters.

Example: If the current page articles\energy, I need to build article\energy\topweekand article\energy\latestpopular.

Where the category should be dynamic based on the current page. preferably on a partial image, so I can use it on different pages.

+3
source share
3 answers

, :

public class ArticleLinksControl {
    public string CategoryName { get; set; }
}

: <%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Namespace.ArticleLinksControl>" %>

, ArticleController , , :

<%Html.RenderPartial("~/Views/Shared/YourControl.ascx",
    new NameSpace.ArticleLinksControl { 
        CategoryName = Model.Category}); %>

usercontrol Model.CategoryName.

, usercontrol . Html- .

+1

UrlHelper URL , URL, .

string url = "~\articles\film\topweek";
string[] parts = url.Split("\\");
string cat = parts[2];
string fil = parts[3];

string actionUrl = UrlHelper.RouteUrl("ActionFilter", new { category = cat, filter = fil });
+1

URL- Html.RouteLink( Html.ActionLink).

<% Html.RouteLink('link text', routeName, new { filter = "topweek" }) %>
0

Source: https://habr.com/ru/post/1707217/


All Articles