When my SEO text slug in the URL ends with a dot symbol, I get a 404 error

℡╲_俬逩灬. 提交于 2019-12-13 07:26:09

问题


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.



来源:https://stackoverflow.com/questions/11487181/when-my-seo-text-slug-in-the-url-ends-with-a-dot-symbol-i-get-a-404-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!