When my SEO text slug in the URL ends with a dot s

2019-09-04 04:51发布

问题:

Quite strange!

Here is the custom route:

        routes.MapRoute(
            "Dota2-News-Details", // Route name
            "dota2-news/{id}/{slug}", // URL with parameters
            new { controller = "Dota2News", action = "Detail", slug = "", id = UrlParameter.Optional } // Parameter defaults
        );

Working links:

http://localhost:20099/dota2-news/3/Some-random-post
http://localhost:20099/dota2-news/5/Another-random-post
http://localhost:20099/dota2-news/14/New-delicious-items

Non-working links:

http://localhost:20099/dota2-news/4/The-Dark-Moon-comes.
http://localhost:20099/dota2-news/11/Bwa-ha-welcome.

On a whim, I went in the database and modified the SEOTextSlug to remove the final . symbol from the value. As expected, it now works properly. I'm confused though, I thought URL's could end with a . symbol.

Here's my Controller:

public ActionResult Detail(int id, string slug)
{
    var viewModel = new OfficialNewsModel();
    var news = _officialNewsRepository.FindById(id);

    if (news.SEOTextSlug != slug)
    {
        return RedirectToAction("Detail", "Dota2News", new { id = news.OfficialNewsId, slug = news.SEOTextSlug });
    }

    viewModel = Mapper.Map<OfficialNew, OfficialNewsModel>(news);
    return View(viewModel);
}

My question:

Why does the trailing . symbol, break the app? A URL can't have a doy symbol anywhere in it?

Server Error in '/' Application. The resource cannot be found. Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /dota2-news/1/In-defense-of-the-Temple.

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.225

回答1:

A URL can't have a doy symbol anywhere in it?

It can. Not at the end though out-of-the-box.

So to allow it add the following to your web.config (<system.web> section):

<httpRuntime relaxedUrlToFileSystemMapping="true" />

Haacked blogged about it. Hanselman also. If you are a .NET developer and haven't yet subscribed to those 2 blogs and read them on a daily basis you will probably miss lots of useful things.