Search code examples
c#asp.net-mvclinqhtml-helper

compere string to string after removing string characters


i have a string that i use as url parameter s query and i need to get all the data that is relevant in order to get a "clean Url" i have remove all characters with helper but i cant compere it to my Db string Update With the code now it's 0 results `

View:

<a href="@Url.Action("Index","schools",
    new { @venue =Html.ResolveSubjectForUrl( gig.Venue)})">@gig.Venue
</a> 

Controller:

public ActionResult Index(string venue)
{
    var testq = _context.LectureGigs.Where(v => v.Venue.StartsWith(venue));
    /just made the query shorter for clear code 
}

public static class UrlExtensions
{
    public static string ResolveSubjectForUrl(this HtmlHelper source, string subject)
    {
        return Regex.Replace(Regex.Replace(subject, "[^\\w]", ""), "[-]{2,}", "");
    }
}

Solution

  • The easiest way to solve this would be to store your 'Resolved' string in the database as part of the venue.

    For example:

    class Venue
    {
        public string Name { get; set; }
        public string Slug { get; set; }
    }
    

    If you store this in a table that is something like:

    CREATE TABLE Venue
    (
        [Name] nvarchar(512) NOT NULL,
        [Slug] nvarchar(512) NOT NULL PRIMARY KEY
    )
    

    Then finding the gigs for that venue becomes as simple as:

    _context.LectureGigs.Where(v => v.Venue.Slug.StartsWith(slug));
    

    Obviously, when you save your Venue you need to populate the Slug value with the value from ResolveSubjectForUrl (which you can use as string extension or a static method without the HtmlHelper parameter)