ASP.NET MVC reuse code between different website - c#

I have two asp.net mvc website, they have the same page to update account data.
How to separate the repeated code(controller, *.cshtml, *.js, *.css... etc) for better maintainability? I don't want to modify code from one website and copy it to the other.
I have tried creating new website project only contains account pages, but the static files(*.js, *.css) can't be reused in this way.

Related

How to dynamically serve static files?

I'm building a ASP.Net Core project which will be used as an online-help for my app and it will serve static files which will display in-app contextual help.
However, one requirement is that each customer can have his own customized help. If he doesn't it should fall back to the default help page.
So I create a WebApplication project as a start. I would need to create a Controller with just one action that receives 2 parameters (customerId, helpId).
Where should the static files be kept (under wwwroot or Pages)?

Updating a session in a web forms site from sub MVC site

Let me paint an ugly picture.
The primary application I maintain is written in C#, webforms. Any new development I carry out is done in MVC, EF, etc.
So 90% of the site is still in webforms, 10% MVC.
The webforms site uses certain user controls to select the item the user wants information for. Think of it as an event, they select event 1, then all pages display information for that event. These settings/selections are held in a cookie and/or session variable.
My issue is, I am slowly converting the site to MVC and so I need to be able to update the selected event from the MVC view, but have that selection "known" in the webforms site.
Both are 2 different codebases.
The webforms code which updates the event is:
Admin.AdminSession.GetCurrent().EventID = selectedEventID;
GetCurrent() returns this
get { return (AdminSession)HttpContext.Current.Session["AdminSession.Current"]; }
But my MVC app cannot seem to read from the session. I have tried
HttpContext.Session["AdminSession.Current"];
and
this.ControllerContext.HttpContext.Session["AdminSession.Current"];
But both yield null
So, is there a way to read a session from a sub app? For context, the hierarchy is like this
rootsite.com (webforms)
rootsite.com/subfolder (mvc project)

Separate MVC application as a folder using Handler Mapping

We have a legacy webforms app and a new section of our site developed with MVC. It works a treat as an application in the Default Web Site in IIS.
Only downside is that the root site's session object isn't passed to the application. We have a work around for this.
A consultant is convinced that we don't need to set it up as an application and it can all be done via handlers and route mappings. He conveniently has not provided us with any details though and I have been tasked to implement it.
The best I have achieved was to add a Module Mapping to the Handler Mapping section in IIS. This filters coolmvcapp/* with Module isapimodule to a virtual directory who's physical path points to our new MVC app. The result is an empty page with a 200 response. Most other combinations I have tried resulted in a variety of errors. (Turns out an empty page is returned without adding the handler mapping - hey ho)
So, is this approach even possible?
I am aware that MVC and webforms can be merged together but for reasons this is something that we want to avoid.

Is there a way to have IIS default document (default.asp) take precedence over .Net routing?

I am trying to migrate a site from classic ASP to .Net (using WebMatrix). I am planning on mixing the new .cshmtl pages in with the legacy .asp files and slowly migrate the site over time. To date, I have individual .asp pages for each piece of content (each url). As I move to .Net, the content will be coming from a database, so there will not be specific pages for each url. I love the power of routing that is available in .Net and it works well and plays nicely with the classic ASP with one exception so far.
My example is that I have an ASP page located at /crafts/default.asp and currently reference that everywhere as just /crafts/. The IIS default document setting takes care of serving the default.asp page when i just reference /crafts/. I want to start to post new items into this area of the site but run as .Net pages. so I created a /crafts.cshtml page which is designed to handle different urls and go to the database and lookup the item and display the info. So as an example I have legacy page at /crafts/fall-crafts.asp and this works fine - because there is an .asp page sitting there named that. But when i reference /crafts/ .Net takes over and serves the /crafts.cshtml page instead of serving the /crafts/default.asp file. Is there something I can do to keep the power of .Net routing in place, but still have IIS serve the default document in this case?
Modify your RouteConfig
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("crafts/");
...
See RouteCollection.Ignore for documentation.
Similar question (mentions some other possibly needed workarounds): Ignore folder in ASP .NET MVC
The default "routing" built into the Web Pages framework relies on matching incoming URLs to physical files on disk, looking first for the existence of cshtml or vbhtml files. All the time you have a crafts.cshtml file at the same level as a folder called crafts, the file will be served first. The solution is to remove or rename you crafts.cshtml file, but currently that will mess up your URLs.
There is a package which offers greater management over routing in Web Pages applications. It's called Routing For WebPages. It allows you to route URLs to any file name you like. You can download that via the Package Manager or Nuget and read this article that I put together that explains how to use it: http://www.mikesdotnetting.com/Article/187/More-Flexible-Routing-For-ASP.NET-Web-Pages.

How to create aspx pages into domain?

I have users, they are signing in system. They have developed pages (using edit button and then press save button) but this is my CMS pages. I want to copy my single pages user by user to publish them. How to create dynamic aspx pages, with config files etc? How to add domain folder?
Generally there's no need to create aspx page or static html pages per user. What you need to do is store his/her post body and other necessary info in db and then create a main page to place user-specific data (like post body, posted-by,..) on it.
For example, you may have a route like this:
/post.aspx?userId=1
This page pulls data from database for the user of Id=1.

Categories

Resources