add additional routing

Feb 27, 2015 at 8:20 AM
Hello,
I need some help with adding an additional routing to the _AppStart file

I have a table my_news with news with the following values
id,Title,Description,DatePublished

I have on page called news with a list of my news and on called newsdetails with the details of each selected news

I want to route my url better than newsdetails?id=7 perharps like newsdetails/{id}/{title}

I add to a new route to my _AppStart page:
RouteTable.Routes.MapWebPageRoute("newsdetails/{id}/{title}", "~/newsdetails.cshtml", new {id = "1", title="default" });

I make my link look like :
<a href="~/newsdetails/@n.id/@n.Title" > in my news page

In my news details page I add this:

@{
var nId = Context.GetRouteValue("id");
var nTitle = Context.GetRouteValue("title");

if (nId == null || nTitle == null)
{
Response.Redirect("~/");
}

var db = Database.Open("mydatabase");
var news = db.QuerySingle("SELECT * FROM my_news Where Id=@0 AND Title=@1", nId, nTitle);

var id = news.Id;
var Title= news.Title;
var Description = news.Description;
var DatePublished = news.DatePublished;

}
And then display my data like this
                <div>
                    <h3>@Title</h3>
                    <p>@Description</p>
                    <p>@DatePublished</p>                   
                </div>
My problem is that I get an error

Object reference not set to an instance of an object.
And points to: var nId = Context.GetRouteValue("id");

Any ideas?
Thanks
Feb 27, 2015 at 2:41 PM
I did small test and works just fine. Make sure to put this :
RouteTable.Routes.MapWebPageRoute("newsdetails/{id}/{title}", "~/newsdetails.cshtml", new {id = "1", title="default" });

as first rule in _AppStart - so it must be before the default route
Feb 27, 2015 at 4:08 PM
Edited Feb 27, 2015 at 4:11 PM
Hello, thanks

I made your change to put at first the RouteTable and now I have this:

At the local IIS express I get: The route handler 'WebPagesRouteHandler' did not return an IHttpHandler from its GetHttpHandler() method.
This is probably some error about the .net framework in IIS

And at the hosting provider I get the rcError page: rcError?aspxerrorpath=/newsdetails/8/testnews

Also at one other website(not razorC) with only on RouteTable it is working just fine

Should I change something else

Thanks again, Emmanuel
Feb 27, 2015 at 9:22 PM
1) make sure you are running correct version of .net
2) found post where user did get that and after setting correct permissions for IIS_IUSRS read/write it started to work
Mar 5, 2015 at 8:34 PM
Did you fix it ? is it working ? What was the problem ?
Mar 6, 2015 at 6:55 AM
Thanks for your interest

Unfortunately I can not manage to get the route to work
Locally I have to install iis to make all the tests with the framework.
On the express I use both .net4 integrated and .net4 classic with the following error
WebPagesRouteHandler' did not return an IHttpHandler from its GetHttpHandler() method.
On my hosting provider I cannot change anything

Thanks, Emmanuel
Mar 7, 2015 at 10:21 AM
Edited Mar 7, 2015 at 10:23 AM
I made another try today and I did the following:

So I didn't mention that before I have 2 pages one the news and one other the newsdetails. In the newsdetails page I include a widget with the the following code
@{Layout = "/rcLayouts/_rcDefault.cshtml";}
@section head{}
@{
var nId = Context.GetRouteValue("id");
var nTitle = Context.GetRouteValue("title");

if (nId == null || nTitle == null)
{Response.Redirect("~/");}
var db = Database.Open("database");
var news = db.QuerySingle("SELECT * FROM TableNews Where Id=@0 AND Title=@1", nId, nTitle);

var id = news.Id;
var Title= news.Title;
var Description = news.Description;
var DatePublished = news.DatePublished;
}
<div> <h3>@Title</h3> <p>@Description</p><p>@DatePublished</p> </div> So with this I get the error: The route handler 'WebPagesRouteHandler' did not return an IHttpHandler from its GetHttpHandler() method.

If I delete the page then create a new page (newsdetails) in the root of my site I am able to get the route to work
So my conclusion is the route is unable to work for some reason when creating a page from the "Pages" and include to this "Page" the widget with the code.

Do you think you can find a solution to this
Thanks, Emmanuel